#!/usr/bin/env python3
"""Seed synthetic projects for each side. Requires OpenCode 2.4.2 and a working
OpenCode provider configuration. Invoke with --root SCRATCH --binary OPENCODE.
Then start each checkout with T3CODE_DEV_SEED=0 vp run dev, pair its web client,
add OpenCode with binary path SCRATCH/{side}/opencode, and add letter-demo.
Use anthropic/claude-haiku-5-5. Run /pick, click A; confirm ANSWER RECEIVED: A.
Run /askwait in a new thread; once its question appears, record, click A,
and press Stop if still working. On head, run /askwait in another new thread
and press Stop while its question is pending. Record through the final state.
Use a 1280x800 dark browser with sidebar and thread details collapsed.
"""
import argparse, json, shlex, subprocess
from pathlib import Path
p=argparse.ArgumentParser()
p.add_argument('--root',type=Path,required=True)
p.add_argument('--binary',type=Path,required=True)
p.add_argument('--config',type=Path,default=Path.home()/'.config/opencode/opencode.json')
a=p.parse_args()
PLUGIN='import { Plugin } from "@opencode/plugin";\nexport default Plugin.define({\n  id: "askwait-demo",\n  setup: async (ctx) => {\n    await ctx.command.transform((commands) => {\n      commands.add({\n        name: "askwait",\n        description: "Ask a question and wait for the answer",\n        execute: async ({ sessionID }) => {\n          await ctx.session.prompt({ sessionID, text: "Use the question tool now to ask exactly one question: Pick a letter, with choices A and B. After receiving the answer, reply with ANSWER RECEIVED: followed by the selected letter. Do not use any other tool." });\n          await ctx.session.wait({ sessionID });\n        },\n      });\n    });\n  },\n});\n'
CONTROL='Use the question tool now to ask exactly one question: Pick a letter, with choices A and B. After receiving the answer, reply with ANSWER RECEIVED: followed by the selected letter. Do not use any other tool.\n'
for side in ['base','head']:
    root=a.root/side
    project=root/'letter-demo'
    (project/'.opencode/plugins').mkdir(parents=True,exist_ok=True)
    (project/'.opencode/command').mkdir(parents=True,exist_ok=True)
    (project/'README.md').write_text('# Letter demo\nSynthetic project for interactive commands.\n')
    (project/'.opencode/plugins/askwait.ts').write_text(PLUGIN)
    (project/'.opencode/command/pick.md').write_text(CONTROL)
    config=root/'xdg-config/opencode'
    config.mkdir(parents=True,exist_ok=True)
    source=json.loads(a.config.read_text())
    source.pop('mcp',None)
    source.pop('tools',None)
    (config/'opencode.json').write_text(json.dumps(source))
    wrapper='#!/bin/sh\n'
    for key,folder in [('CONFIG','config'),('DATA','data'),('CACHE','cache'),('STATE','state')]:
        wrapper+=f'export XDG_{key}_HOME={shlex.quote(str(root/('xdg-'+folder)))}\n'
    wrapper+='exec '+shlex.quote(str(a.binary))+' "$@"\n'
    (root/'opencode').write_text(wrapper)
    (root/'opencode').chmod(0o755)
    if not (project/'.git').exists():
        subprocess.run(['git','init',str(project)],check=True)
