#!/usr/bin/env bash
# Spawn 5 claude sessions in tmux, one per Bucket-E HS subsystem.
# Each runs in its own git worktree rooted at /root/rose-ash-e/<item>,
# on branch hs-e<N>, rebased onto loops/hs.
#
# Usage: ./scripts/sx-hs-e-up.sh [interval]
#   interval defaults to self-paced (omit to let model decide)
#
# After the script prints done:
#   tmux a -t sx-hs-e
#   Ctrl-B + <0..4> to switch (0=e36 ... 4=e40)
#   Ctrl-B + d to detach
#
# Stop: ./scripts/sx-hs-e-down.sh
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
SESSION="sx-hs-e"
WORKTREE_BASE="/root/rose-ash-e"
INTERVAL="${1:-}"
BOOT_WAIT=20

if tmux has-session -t "$SESSION" 2>/dev/null; then
  echo "Session '$SESSION' already exists."
  echo "  Attach:  tmux a -t $SESSION"
  echo "  Kill:    ./scripts/sx-hs-e-down.sh"
  exit 1
fi

declare -A DESIGN=(
  [e36]=e36-websocket.md
  [e37]=e37-tokenizer-api.md
  [e38]=e38-sourceinfo.md
  [e39]=e39-webworker.md
  [e40]=e40-real-fetch.md
)

declare -A BRANCH=(
  [e36]=hs-e36-websocket
  [e37]=hs-e37-tokenizer
  [e38]=hs-e38-sourceinfo
  [e39]=hs-e39-webworker
  [e40]=hs-e40-fetch
)

declare -A LABEL=(
  [e36]="E36 WebSocket (+16)"
  [e37]="E37 Tokenizer-as-API (+17)"
  [e38]="E38 SourceInfo (+4)"
  [e39]="E39 WebWorker (+1)"
  [e40]="E40 Fetch/non-2xx (+7)"
)

ORDER=(e36 e37 e38 e39 e40)

write_worktree_settings() {
  local wt="$1"
  local settings_dir="$wt/.claude"
  mkdir -p "$settings_dir"
  cat > "$settings_dir/settings.local.json" <<'SETTINGS'
{
  "permissions": {
    "allow": [
      "mcp__sx-tree__sx_summarise",
      "mcp__sx-tree__sx_read_tree",
      "mcp__sx-tree__sx_read_subtree",
      "mcp__sx-tree__sx_get_context",
      "mcp__sx-tree__sx_find_all",
      "mcp__sx-tree__sx_find_across",
      "mcp__sx-tree__sx_get_siblings",
      "mcp__sx-tree__sx_validate",
      "mcp__sx-tree__sx_replace_node",
      "mcp__sx-tree__sx_insert_child",
      "mcp__sx-tree__sx_insert_near",
      "mcp__sx-tree__sx_delete_node",
      "mcp__sx-tree__sx_wrap_node",
      "mcp__sx-tree__sx_rename_symbol",
      "mcp__sx-tree__sx_replace_by_pattern",
      "mcp__sx-tree__sx_rename_across",
      "mcp__sx-tree__sx_write_file",
      "mcp__sx-tree__sx_pretty_print",
      "mcp__sx-tree__sx_eval",
      "mcp__sx-tree__sx_harness_eval",
      "mcp__sx-tree__sx_macroexpand",
      "mcp__sx-tree__sx_trace",
      "mcp__sx-tree__sx_deps",
      "mcp__sx-tree__sx_diff",
      "mcp__sx-tree__sx_diff_branch",
      "mcp__sx-tree__sx_changed",
      "mcp__sx-tree__sx_blame",
      "mcp__sx-tree__sx_build",
      "mcp__sx-tree__sx_build_manifest",
      "mcp__sx-tree__sx_build_bytecode",
      "mcp__sx-tree__sx_test",
      "mcp__sx-tree__sx_format_check",
      "mcp__sx-tree__sx_comp_list",
      "mcp__sx-tree__sx_comp_usage",
      "mcp__sx-tree__sx_nav",
      "mcp__sx-tree__sx_env",
      "mcp__sx-tree__sx_playwright",
      "mcp__hs-test__hs_test_run",
      "mcp__hs-test__hs_test_regen",
      "mcp__hs-test__hs_test_kill",
      "mcp__hs-test__hs_test_status",
      "Bash(node *)",
      "Bash(python3 *)",
      "Bash(bash *)",
      "Bash(cp *)",
      "Bash(git *)"
    ]
  },
  "enabledMcpjsonServers": [
    "sx-tree",
    "rose-ash-services",
    "hs-test"
  ]
}
SETTINGS
}

echo "Preparing Bucket-E worktrees under $WORKTREE_BASE ..."
mkdir -p "$WORKTREE_BASE"

for item in "${ORDER[@]}"; do
  wt="$WORKTREE_BASE/$item"
  branch="${BRANCH[$item]}"

  # Create or reset implementation branch from loops/hs
  if git show-ref --verify --quiet "refs/heads/$branch"; then
    echo "  $item: branch $branch exists"
  else
    git branch "$branch" loops/hs
    echo "  $item: created branch $branch from loops/hs"
  fi

  # Add worktree
  if [ -d "$wt/.git" ] || [ -f "$wt/.git" ]; then
    echo "  $item: worktree exists at $wt"
  else
    git worktree add "$wt" "$branch"
    echo "  $item: worktree created at $wt"
  fi

  write_worktree_settings "$wt"
done

# Create tmux session
tmux new-session -d -s "$SESSION" -n "${ORDER[0]}" -c "$WORKTREE_BASE/${ORDER[0]}"
for item in "${ORDER[@]:1}"; do
  tmux new-window -t "$SESSION" -n "$item" -c "$WORKTREE_BASE/$item"
done

echo "Starting ${#ORDER[@]} claude sessions..."
for item in "${ORDER[@]}"; do
  tmux send-keys -t "$SESSION:$item" "claude" C-m
done

echo "Waiting ${BOOT_WAIT}s for claude to boot..."
sleep "$BOOT_WAIT"

for item in "${ORDER[@]}"; do
  design="${DESIGN[$item]}"
  label="${LABEL[$item]}"
  branch="${BRANCH[$item]}"

  if [ -n "$INTERVAL" ]; then
    preamble="/loop $INTERVAL "
  else
    preamble="/loop "
  fi

  cmd="${preamble}You are implementing HS conformance Bucket-E item ${label}. Read plans/designs/${design} carefully — it is your complete spec. Do ONE piece of work per fire: implement the next unimplemented step from the design doc, run the relevant hs_test_run suite to verify, commit with a short factual message, then stop. Scope: lib/hyperscript/**, tests/playwright/generate-sx-tests.py, tests/hs-run-filtered.js only — never touch spec/, hosts/, shared/ kernel, or other lib/<lang>/. Use sx-tree MCP for all .sx edits. You are on branch ${branch} in worktree /root/rose-ash-e/${item}; push commits to origin/${branch} (never main or loops/hs)."

  tmux send-keys -t "$SESSION:$item" "$cmd"
  sleep 0.5
  tmux send-keys -t "$SESSION:$item" Enter
done

echo ""
echo "Done. 5 Bucket-E loops started in tmux session '$SESSION'."
echo ""
echo "  Attach:    tmux a -t $SESSION"
echo "  Switch:    Ctrl-B <0..4>  (0=e36 1=e37 2=e38 3=e39 4=e40)"
echo "  List:      Ctrl-B w"
echo "  Detach:    Ctrl-B d"
echo "  Stop:      ./scripts/sx-hs-e-down.sh"
echo ""
echo "Worktrees:"
git worktree list | grep rose-ash-e || true
