#!/usr/bin/env bash
# Run from either commit's checkout. Use a fresh DATA_DIR for each commit/mode.
set -eu
: "${DATA_DIR:?Set a fresh, isolated data directory}"
: "${DEMO_DIR:?Set the synthetic demo repository path}"
mode=${1:-lan}
mkdir -p "$DEMO_DIR"
git init -q --initial-branch=main "$DEMO_DIR"
if [ "$mode" = tunnel ]; then
  : "${CONNECT_STAGE_ENV:?Path to the authorized T3 Connect test-stage env}"
  set -a
  . "$CONNECT_STAGE_ENV"
  set +a
  node apps/server/src/bin.ts connect login --headless --base-dir "$DATA_DIR"
  # Approve the login in a browser with the designated stage account.
  node apps/server/src/bin.ts connect link --headless --base-dir "$DATA_DIR"
fi
# Keep this in one terminal. In another terminal after startup:
# node apps/server/src/bin.ts project add "$DEMO_DIR" --title demo --base-dir "$DATA_DIR"
# Open the printed pairing link, then /settings/access.
# Tunnel: wait for real reachability, inspect the MCP row, click Copy and paste.
# LAN: run a web frontend with HOST=0.0.0.0 PORT=<free port>
# T3CODE_PORT=<backend port> T3CODE_SINGLE_ORIGIN_DEV=1 vp dev from apps/web.
# Pair through the LAN web origin and open /settings/access: no MCP row.
# Viewport 1280x800 CSS pixels, light theme. Capture after two identical stills.
# At end, for tunnel: node apps/server/src/bin.ts connect logout --base-dir "$DATA_DIR"
if [ "$mode" = tunnel ]; then
  T3CODE_DEV_SEED=0 T3CODE_ENVIRONMENT_LABEL=Scenario T3CODE_ENVIRONMENT_MACHINE_KIND=laptop \
    vp run dev --home-dir "$DATA_DIR" --no-seed
else
  T3CODE_DEV_SEED=0 T3CODE_ENVIRONMENT_LABEL=Scenario T3CODE_ENVIRONMENT_MACHINE_KIND=laptop \
    vp run dev --host 0.0.0.0 --home-dir "$DATA_DIR" --no-seed
fi
