"""Reproduction seed recipe, assembled from the commands used for both captures.

Run before starting each dev server, then start with T3CODE_DEV_SEED=0 vp run dev.
After startup run this file again with --parent to create the real parent turn.
Supply a Claude settings file containing your own authentication environment.
Never upload that settings file or the resulting private Claude directory.
"""
import argparse
import json
import pathlib
import shlex
import shutil
import subprocess

p = argparse.ArgumentParser()
p.add_argument('checkout', type=pathlib.Path)
p.add_argument('run_dir', type=pathlib.Path)
p.add_argument('--auth-settings', type=pathlib.Path)
p.add_argument('--parent', action='store_true')
a = p.parse_args()
a.checkout = a.checkout.resolve()
a.run_dir = a.run_dir.resolve()
a.run_dir.mkdir(parents=True, exist_ok=True)
private = a.run_dir / 'private-claude'
private.mkdir(exist_ok=True)
if a.auth_settings:
    shutil.copyfile(a.auth_settings, private / 'settings.json')
wrapper = a.run_dir / 'claude-wrapper'
wrapper.write_text('#!/bin/sh\nexport CLAUDE_CONFIG_DIR=' + shlex.quote(str(private)) + '\nexec ' + shlex.quote(shutil.which('claude')) + ' "$@"\n')
wrapper.chmod(0o700)
settings = a.checkout / '.t3/userdata/settings.json'
settings.parent.mkdir(parents=True, exist_ok=True)
config = json.loads(settings.read_text()) if settings.exists() else {}
config['providerInstances'] = {'sandboxClaude': {'driver': 'claudeAgent', 'displayName': 'Sandbox Claude', 'config': {'binaryPath': str(wrapper)}}}
settings.write_text(json.dumps(config))
demo = a.run_dir / 'demo'
if not demo.exists():
    demo.mkdir()
    (demo / 'README.md').write_text('Synthetic Side Chat proof project.\n')
    subprocess.run(['git', 'init', '-q', '--initial-branch=main', str(demo)], check=True)
    subprocess.run(['git', '-C', str(demo), 'add', '.'], check=True)
    subprocess.run(['git', '-C', str(demo), '-c', 'user.name=Proof', '-c', 'user.email=proof@example.invalid', 'commit', '-qm', 'Initial'], check=True)
if a.parent:
    def cli(*args):
        subprocess.run(['node', 'apps/server/src/bin.ts', *args, '--base-dir', '.t3'], cwd=a.checkout, check=True)
    cli('project', 'add', str(demo), '--title', 'Side Chat proof')
    cli('thread', 'start', str(demo), 'Reply with just the word PARENT', '--provider', 'sandboxClaude', '--model', 'claude-haiku-5-5', '--title', 'Sandbox parent', '--wait', '--json')
