#!/usr/bin/env bash
set -euo pipefail
checkout=$1
side=$2
run_dir=/tmp/lead-33038-r2/capture/seed-$side-$(date -u +%Y%m%dT%H%M%SZ)
mkdir -p "$run_dir" /tmp/lead-33038-r2/capture/fixture-project
if [ ! -d /tmp/lead-33038-r2/capture/fixture-project/.git ]; then
 git -C /tmp/lead-33038-r2/capture/fixture-project init -q
fi
cd "$checkout"
node apps/server/src/bin.ts project add --base-dir .t3 /tmp/lead-33038-r2/capture/fixture-project > "$run_dir/project.json"
for scene in A B; do
 count=9
 [ "$scene" != Two ] || count=2
 node apps/server/src/bin.ts thread start --base-dir .t3 --provider devReplay --model multi-turn --wait --json --title "Minimap $scene" /tmp/lead-33038-r2/capture/fixture-project 'Turn 1: hello' > "$run_dir/$scene-start.json"
 tid=$(head -1 "$run_dir/$scene-start.json" | python3 -I -c 'import json,sys;print(json.load(sys.stdin)["threadId"])')
 printf '%s\n' "$tid" > "$run_dir/$scene-id.txt"
 for ((i=2;i<=count;i++)); do
  node apps/server/src/bin.ts thread send --base-dir .t3 --wait "$tid" "Turn $i: next step" > "$run_dir/$scene-turn-$i.txt"
 done
done
printf '%s\n' "$run_dir"
