#!/usr/bin/env bash
# agent-dispatch.sh — Generate a self-contained prompt for any agent
#
# Usage: agent-dispatch.sh <agent_name> <issue_num> <repo>
#   agent-dispatch.sh manus 42 Timmy_Foundation/the-nexus
#
# Outputs a prompt to stdout. Copy-paste into the agent's interface.
# The prompt includes everything: API URLs, token, git commands, PR creation.

set -euo pipefail

AGENT_NAME="${1:?Usage: agent-dispatch.sh <agent> <issue_num> <owner/repo>}"
ISSUE_NUM="${2:?Usage: agent-dispatch.sh <agent> <issue_num> <owner/repo>}"
REPO="${3:?Usage: agent-dispatch.sh <agent> <issue_num> <owner/repo>}"

GITEA_URL="http://143.198.27.163:3000"
TOKEN_FILE="$HOME/.hermes/${AGENT_NAME}_token"

if [ ! -f "$TOKEN_FILE" ]; then
  echo "ERROR: No token found at $TOKEN_FILE" >&2
  echo "Create a Gitea user and token for '$AGENT_NAME' first." >&2
  exit 1
fi

GITEA_TOKEN=$(cat "$TOKEN_FILE")
REPO_OWNER=$(echo "$REPO" | cut -d/ -f1)
REPO_NAME=$(echo "$REPO" | cut -d/ -f2)
BRANCH="${AGENT_NAME}/issue-${ISSUE_NUM}"

# Fetch issue title
ISSUE_TITLE=$(curl -sf -H "Authorization: token $GITEA_TOKEN" \
  "${GITEA_URL}/api/v1/repos/${REPO}/issues/${ISSUE_NUM}" 2>/dev/null | \
  python3 -c "import sys,json; print(json.loads(sys.stdin.read())['title'])" 2>/dev/null || echo "Issue #${ISSUE_NUM}")

cat <<PROMPT
You are ${AGENT_NAME}, an autonomous code agent working on the ${REPO_NAME} project.

YOUR ISSUE: #${ISSUE_NUM} — "${ISSUE_TITLE}"

GITEA API: ${GITEA_URL}/api/v1
GITEA TOKEN: ${GITEA_TOKEN}
REPO: ${REPO_OWNER}/${REPO_NAME}

== STEP 1: READ THE ISSUE ==

curl -s -H "Authorization: token ${GITEA_TOKEN}" "${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/issues/${ISSUE_NUM}"
curl -s -H "Authorization: token ${GITEA_TOKEN}" "${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/issues/${ISSUE_NUM}/comments"

Read the issue body AND all comments for context and build order constraints.

== STEP 2: SET UP WORKSPACE ==

git clone http://${AGENT_NAME}:${GITEA_TOKEN}@143.198.27.163:3000/${REPO_OWNER}/${REPO_NAME}.git /tmp/${AGENT_NAME}-work-${ISSUE_NUM}
cd /tmp/${AGENT_NAME}-work-${ISSUE_NUM}

Check if branch exists (prior attempt): git ls-remote origin ${BRANCH}
If yes: git fetch origin ${BRANCH} && git checkout ${BRANCH}
If no:  git checkout -b ${BRANCH}

== STEP 3: UNDERSTAND THE PROJECT ==

Read README.md or any contributing guide. Check for tox.ini, Makefile, package.json.
Follow existing code conventions.

== STEP 4: DO THE WORK ==

Implement the fix/feature described in the issue. Run tests if the project has them.

== STEP 5: COMMIT AND PUSH ==

git add -A
git commit -m "feat: <description> (#${ISSUE_NUM})

Fixes #${ISSUE_NUM}"
git push origin ${BRANCH}

== STEP 6: CREATE PR ==

curl -s -X POST "${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/pulls" \\
  -H "Authorization: token ${GITEA_TOKEN}" \\
  -H "Content-Type: application/json" \\
  -d '{"title": "[${AGENT_NAME}] <description> (#${ISSUE_NUM})", "body": "Fixes #${ISSUE_NUM}\n\n<describe changes>", "head": "${BRANCH}", "base": "main"}'

== STEP 7: COMMENT ON ISSUE ==

curl -s -X POST "${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/issues/${ISSUE_NUM}/comments" \\
  -H "Authorization: token ${GITEA_TOKEN}" \\
  -H "Content-Type: application/json" \\
  -d '{"body": "PR submitted. <summary>"}'

== RULES ==
- Read project docs FIRST.
- Use the project's own test/lint tools.
- Respect git hooks. Do not skip them.
- If tests fail twice, STOP and comment on the issue.
- ALWAYS push your work. ALWAYS create a PR. No exceptions.
- Clean up: remove /tmp/${AGENT_NAME}-work-${ISSUE_NUM} when done.
PROMPT
