#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel)"
PORT=7331
MODE="manual"

for arg in "$@"; do
  case "$arg" in
    --tmux)   MODE="tmux"   ;;
    --kitty)  MODE="kitty"  ;;
    --manual) MODE="manual" ;;
    *) echo "Unknown option: $arg" >&2; echo "Usage: $0 [--manual|--tmux|--kitty]" >&2; exit 1 ;;
  esac
done

# Port check
if lsof -ti:"$PORT" &>/dev/null; then
  echo "Error: port $PORT is already in use."
  echo "Relay already running? Kill it with: kill \$(lsof -ti:$PORT)"
  exit 1
fi

# Install deps (no-op if node_modules current)
cd "$SCRIPT_DIR"
npm install --silent

# Discover latest coordination prompts for instructions
COORD_DIR="$REPO_ROOT/docs/superpowers/coordination"
PM_PROMPT="$(ls -t "$COORD_DIR"/*-pm-prompt.md 2>/dev/null | head -1 || echo "(none found — run multi-agent-kickoff skill first)")"
DEV_A_PROMPT="$(ls -t "$COORD_DIR"/*-dev-a-prompt.md 2>/dev/null | head -1 || echo "(none found)")"
DEV_B_PROMPT="$(ls -t "$COORD_DIR"/*-dev-b-prompt.md 2>/dev/null | head -1 || echo "(none found)")"
DEV_C_PROMPT="$(ls -t "$COORD_DIR"/*-dev-c-prompt.md 2>/dev/null | head -1 || echo "(none found)")"

print_manual_instructions() {
  echo ""
  echo "╔══════════════════════════════════════════════════════════════╗"
  echo "║          RELAY SERVER — MULTI-AGENT LIFT LAUNCHER            ║"
  echo "╚══════════════════════════════════════════════════════════════╝"
  echo ""
  echo "Open 4 new terminals. In each, start Claude Code and paste"
  echo "the content BELOW the '---' line from the corresponding file."
  echo ""
  echo "  Terminal 1 (PM):    cat '$PM_PROMPT'"
  echo "  Terminal 2 (Dev A): cat '$DEV_A_PROMPT'"
  echo "  Terminal 3 (Dev B): cat '$DEV_B_PROMPT'"
  echo "  Terminal 4 (Dev C): cat '$DEV_C_PROMPT'"
  echo ""
  echo "This terminal becomes the relay log. Keep it open."
  echo ""
  echo "══════════════════════════════════════════════════════════════"
}

launch_tmux() {
  SESSION="relay-lift"
  tmux new-session -d -s "$SESSION" -n "relay" \
    "cd '$SCRIPT_DIR' && npx tsx server.ts"
  tmux new-window -t "$SESSION:" -n "pm" "cd '$REPO_ROOT' && claude"
  tmux new-window -t "$SESSION:" -n "dev-a" "cd '$REPO_ROOT' && claude"
  tmux new-window -t "$SESSION:" -n "dev-b" "cd '$REPO_ROOT' && claude"
  tmux new-window -t "$SESSION:" -n "dev-c" "cd '$REPO_ROOT' && claude"
  echo ""
  echo "[relay] Opened tmux session '$SESSION' with 5 windows: relay, pm, dev-a, dev-b, dev-c."
  echo "[relay] Paste the kickoff prompt into each Claude window."
  echo "        Prompts:"
  echo "          PM:    $PM_PROMPT"
  echo "          Dev A: $DEV_A_PROMPT"
  echo "          Dev B: $DEV_B_PROMPT"
  echo "          Dev C: $DEV_C_PROMPT"
  echo ""
  tmux attach-session -t "$SESSION"
}

launch_kitty() {
  kitty @ launch --type=tab --tab-title "relay" -- \
    bash -c "cd '$SCRIPT_DIR' && npx tsx server.ts"
  kitty @ launch --type=tab --tab-title "PM" --hold -- \
    bash -l -i -c "cd '$REPO_ROOT' && claude"
  kitty @ launch --type=tab --tab-title "Dev-A" --hold -- \
    bash -l -i -c "cd '$REPO_ROOT' && claude"
  kitty @ launch --type=tab --tab-title "Dev-B" --hold -- \
    bash -l -i -c "cd '$REPO_ROOT' && claude"
  kitty @ launch --type=tab --tab-title "Dev-C" --hold -- \
    bash -l -i -c "cd '$REPO_ROOT' && claude"
  echo ""
  echo "[relay] Opened kitty tab 'relay' + 4 windows (PM, Dev-A, Dev-B, Dev-C)."
  echo "        Paste the kickoff prompts into each Claude window."
  echo "          PM:    $PM_PROMPT"
  echo "          Dev A: $DEV_A_PROMPT"
  echo "          Dev B: $DEV_B_PROMPT"
  echo "          Dev C: $DEV_C_PROMPT"
}

case "$MODE" in
  manual)
    print_manual_instructions
    exec npx tsx "$SCRIPT_DIR/server.ts"
    ;;
  tmux)
    launch_tmux
    ;;
  kitty)
    launch_kitty
    ;;
esac
