"""Run against either checked-out commit after starting its dev server with T3CODE_DEV_SEED=0.
Usage: python3 reproduce.py CHECKOUT T3_HOME SYNTHETIC_PROJECT PROVIDER MODEL
Provider/model are local runtime configuration; do not include them in public captures.
Use a fresh T3 home per commit, identical browser viewport/theme, and record the follow-up.
"""
import json
import pathlib
import subprocess
import sys

checkout, home, project, provider, model = sys.argv[1:]

def cli(*args):
    return subprocess.check_output(
        ['node', 'apps/server/src/bin.ts', *args, '--base-dir', home],
        cwd=checkout, text=True,
    )

pathlib.Path(project).mkdir(parents=True, exist_ok=True)
subprocess.run(['git', 'init', '-q', '-b', 'main', project], check=True)
subprocess.run(['git', '-C', project, '-c', 'user.name=Demo', '-c',
                'user.email=demo@example.invalid', 'commit', '--allow-empty', '-qm', 'Initial'], check=True)
cli('project', 'add', project, '--title', 'Settlement demo')
for title in ['Settlement check', 'Settlement repeat']:
    first = json.loads(cli('thread', 'start', project, 'What is 2 + 2?',
                          '--title', title, '--provider', provider, '--model', model,
                          '--wait', '--json'))
    thread = first['threadId']
    print(title, 'initial:', first['reply'], flush=True)
    input('Open this thread in the web client, hide private runtime metadata, start recording, then Enter: ')
    followup = json.loads(cli('thread', 'send', thread,
                             'Makes sense, thanks. Settle this thread.', '--wait', '--json'))
    state = json.loads(cli('thread', 'show', thread, '--json'))
    print(json.dumps({'title': title, 'reply': followup['reply'],
                      'status': state['thread']['status'], 'settled': state['thread']['settled']}))
