"""Synthetic fixture and real-app CLI driver used for the skill-handoff proof.

Run with: python seed-and-drive.py CHECKOUT OUTPUT_DIRECTORY
The checkout's dev server must already run with T3CODE_DEV_SEED=0.
After the first turn, open the returned thread in the web app, collapse the
thread details panel, select Claude Haiku 5.5, and send exactly `continue`.
Record that interaction. Then run the two printed control commands.
Each invocation makes a fresh timestamped project; no live data is read.
"""
import datetime
import json
import shlex
import subprocess
import sys
from pathlib import Path

checkout = Path(sys.argv[1]).resolve()
output = Path(sys.argv[2]).resolve() / datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')
project = output / 'skill-demo'
project.mkdir(parents=True)
skill = '''---
name: my-skill
description: Echo the explicitly supplied arguments when the user requests my-skill.
---
Reply with SKILLRAN followed by the arguments supplied to this skill. Do not execute any other tools. The arguments are: $ARGUMENTS
'''
for sub in ['.agents/skills/my-skill', '.claude/skills/my-skill']:
    directory = project / sub
    directory.mkdir(parents=True)
    (directory / 'SKILL.md').write_text(skill)
subprocess.run(['git', 'init', '-q', str(project)], check=True)
subprocess.run(['git', '-C', str(project), 'add', '.'], check=True)
subprocess.run(['git', '-C', str(project), '-c', 'user.name=Demo', '-c', 'user.email=demo@example.com', 'commit', '-qm', 'Synthetic skill fixture'], check=True)
cli = ['node', 'apps/server/src/bin.ts']
subprocess.run(cli + ['project', 'add', '--base-dir', '.t3', '--title', 'Skill handoff demo', str(project)], cwd=checkout, check=True)
result = subprocess.run(cli + ['thread', 'start', '--base-dir', '.t3', '--provider', 'codex', '--model', 'gpt-6-luna', '--title', 'Skill handoff', '--wait', '--json', str(project), '$my-skill do something'], cwd=checkout, check=True, text=True, capture_output=True)
(output / 'initial-turn.json').write_text(result.stdout)
thread = json.loads(result.stdout)['threadId']
print('Thread:', thread)
print('Now switch this thread to Claude Haiku 5.5 in the UI and send: continue')
for message in ['$my-skill do something', '$my-skill do something\n\nUser message:\ncontinue']:
    print(shlex.join(cli + ['thread', 'send', '--base-dir', '.t3', '--wait', '--json', thread, message]))
