#!/usr/bin/env bash
# Multi-agent kickoff launcher — v0.9.0 (org GUI + key-file second factor)
# 1 PM + 5 dev sessions (dev-a..dev-e). Starts the relay, opens a tmux session
# with 6 windows, and prints the prompt-paste cheatsheet.
set -e

REPO=/home/alee/Sources/relicario

# ---------------------------------------------------------------------------
# Section 1 — Relay health check and auto-start
# ---------------------------------------------------------------------------
if curl -sf http://127.0.0.1:7331/sse --max-time 2 > /dev/null 2>&1; then
  echo "[relay] already running"
else
  echo "[relay] starting..." && nohup npx tsx "$REPO/tools/relay/server.ts" > /tmp/relay-v0.9.0.log 2>&1 &
  for i in $(seq 1 10); do sleep 1; curl -sf http://127.0.0.1:7331/sse --max-time 1 > /dev/null 2>&1 && echo "[relay] ready" && break || true; [ $i -eq 10 ] && echo "[relay] ERROR — check /tmp/relay-v0.9.0.log" && exit 1; done
fi

# ---------------------------------------------------------------------------
# Section 2 — tmux session
# ---------------------------------------------------------------------------
SESSION="v0.9.0"

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

# Create session with PM window
tmux new-session -d -s "$SESSION" -n "pm" -c "$REPO"
tmux send-keys -t "$SESSION:pm" "claude" Enter

# Dev windows (dev-a..dev-e). Each starts in the MAIN repo; the dev prompt's
# Setup block creates its own worktree as the dev's first action.
for DEV in a b c d e; do
  tmux new-window -t "$SESSION" -n "dev-$DEV" -c "$REPO"
  tmux send-keys -t "$SESSION:dev-$DEV" "claude" Enter
done

# Prompt-paste cheatsheet
echo ""
echo "┌──────────────────────────────────────────────────────────────────────────────┐"
echo "│                     v0.9.0 — prompt-paste cheatsheet                          │"
echo "├──────────┬───────────────────────────────────────────────────────────────────┤"
echo "│ Window   │ Paste file (everything below the '---' line)                      │"
echo "├──────────┼───────────────────────────────────────────────────────────────────┤"
echo "│ pm       │ docs/superpowers/coordination/v0.9.0-pm-prompt.md                 │"
echo "│ dev-a    │ docs/superpowers/coordination/v0.9.0-dev-a-prompt.md  (org found.) │"
echo "│ dev-b    │ docs/superpowers/coordination/v0.9.0-dev-b-prompt.md  (org read)   │"
echo "│ dev-c    │ docs/superpowers/coordination/v0.9.0-dev-c-prompt.md  (org write)  │"
echo "│ dev-d    │ docs/superpowers/coordination/v0.9.0-dev-d-prompt.md  (keyfile)    │"
echo "│ dev-e    │ docs/superpowers/coordination/v0.9.0-dev-e-prompt.md  (keyfile ext)│"
echo "└──────────┴───────────────────────────────────────────────────────────────────┘"
echo ""
echo "Start order (PM gates this): A + D start now; C runs its Task-1 spike now;"
echo "B holds for A; E holds for D; C's writes hold for A + B."
echo ""

tmux attach-session -t "$SESSION"
