#!/usr/bin/env bash
# Auto-generated by multi-agent-kickoff — v0.7.0 (finish the extension restructure)
# Streams: Dev-A = Phase 3, Dev-B = Phase 4, Dev-C = Phase 6
set -e

REPO="/home/alee/Sources/relicario"
RELAY_DIR="$REPO/tools/relay"
COORD="$REPO/docs/superpowers/coordination"
RELEASE="v0.7"
SESSION="$RELEASE"

# ── 1. Relay ─────────────────────────────────────────────────────────────
if curl -sf http://127.0.0.1:7331/sse --max-time 2 > /dev/null 2>&1; then
  echo "[relay] already running on :7331"
else
  echo "[relay] starting..."
  cd "$RELAY_DIR"
  nohup npx tsx server.ts > /tmp/relay-v0.7.log 2>&1 &
  for i in $(seq 1 10); do
    sleep 1
    if curl -sf http://127.0.0.1:7331/sse --max-time 1 > /dev/null 2>&1; then
      echo "[relay] ready on :7331"
      break
    fi
    if [ "$i" -eq 10 ]; then
      echo "[relay] ERROR: failed to start — check /tmp/relay-v0.7.log"
      exit 1
    fi
  done
fi

# ── 2. tmux session ──────────────────────────────────────────────────────
if tmux has-session -t "$SESSION" 2>/dev/null; then
  echo "[tmux] session '$SESSION' already exists — attaching"
  exec tmux attach-session -t "$SESSION"
fi

echo "[tmux] creating session '$SESSION'..."
tmux new-session  -d -s "$SESSION" -n "PM"
tmux send-keys    -t "$SESSION:PM"    "claude" Enter

tmux new-window   -t "$SESSION" -n "Dev-A"
tmux send-keys    -t "$SESSION:Dev-A" "claude" Enter

tmux new-window   -t "$SESSION" -n "Dev-B"
tmux send-keys    -t "$SESSION:Dev-B" "claude" Enter

tmux new-window   -t "$SESSION" -n "Dev-C"
tmux send-keys    -t "$SESSION:Dev-C" "claude" Enter

tmux select-window -t "$SESSION:PM"

echo ""
echo "╔══════════════════════════════════════════════════════════════════════╗"
echo "║  v0.7.0 — finish the extension restructure — prompt cheatsheet        ║"
echo "╠══════════════════════════════════════════════════════════════════════╣"
echo "║  PM window    →  paste $COORD/v0.7-pm-prompt.md      ║"
echo "║  Dev-A window →  paste $COORD/v0.7-dev-a-prompt.md   ║"
echo "║  Dev-B window →  paste $COORD/v0.7-dev-b-prompt.md   ║"
echo "║  Dev-C window →  paste $COORD/v0.7-dev-c-prompt.md   ║"
echo "╠══════════════════════════════════════════════════════════════════════╣"
echo "║  A = Phase 3 (setup wizard SW migration)                              ║"
echo "║  B = Phase 4 (split vault.ts + vault_locked lift)                     ║"
echo "║  C = Phase 6 (get_vault_status + status indicator) — deps on A & B    ║"
echo "╚══════════════════════════════════════════════════════════════════════╝"
echo ""
echo "[tmux] attaching — use Ctrl-b n / Ctrl-b p to switch windows"
exec tmux attach-session -t "$SESSION"
