#!/usr/bin/env bash
set -eu
# Run from the commit's checkout, with its isolated dev server already started:
# T3CODE_DEV_SEED=0 vp run dev
# Usage: bash reproduce-question.sh <project-directory> <isolated-t3-home> <A|B> [thread-A-id]
project=$1
state_home=$2
phase=$3
case "$phase" in
 A)
  mkdir -p "$project"
  git -C "$project" init --quiet --initial-branch=main
  printf 'Synthetic question evidence project.\n' > "$project/README.md"
  git -C "$project" add README.md
  git -C "$project" -c user.name=Evidence -c user.email=evidence@example.invalid commit -qm 'Initial synthetic project'
  node apps/server/src/bin.ts project add "$project" --title 'Color question' --base-dir "$state_home"
  node apps/server/src/bin.ts thread start "$project" "Use the AskUserQuestion tool to ask me: 'Which color?' with options Red and Blue. After I answer, quote my answer back verbatim." --title 'Thread A - color question' --provider claudeAgent --model haiku --wait --json --base-dir "$state_home"
  # Exit 2 means the requested question is pending, as expected.
  ;;
 B)
  target_thread=$4
  node apps/server/src/bin.ts thread start "$project" "Call t3_thread_send exactly once with threadId = $target_thread, mode = auto, message = 'FYI from thread B: the build finished.' Then report its delivery result and stop." --title 'Thread B - build notice' --provider claudeAgent --model haiku --wait --json --base-dir "$state_home"
  ;;
esac
