#!/usr/bin/env python3
"""Recreate the synthetic native-permission fixture (stop the test app first).

python seed.py CHECKOUT PROOF_DIRECTORY OPENCODE_2_BINARY [--check]
Then start CHECKOUT with T3CODE_DEV_SEED=0 vp run dev, and add the printed
project directory using T3's project add command or the UI.
In a new web thread choose OpenCode / Claude Haiku 5.5 / Permissions Auto.
Send: Run exactly this shell command and nothing else: printf native-allow-proof
Capture from Send until approval appears (base) or command completes (head).
Use a 1280x800 light browser; hide sidebar and thread details before recording.
Expand Worked for and the command row after completion to see stdout.
For stable base stills only, pause the decorative live-tool-shine animation;
recordings retain the original animation. Compare two stills 500ms apart.
"""
import argparse
import json
from pathlib import Path
import subprocess

p = argparse.ArgumentParser(description=__doc__)
p.add_argument('checkout', type=Path)
p.add_argument('proof_directory', type=Path)
p.add_argument('opencode_binary', type=Path)
p.add_argument('--check', action='store_true')
a = p.parse_args()
project = a.proof_directory.resolve() / 'project'
settings = a.checkout.resolve() / '.t3/userdata/settings.json'
native = {'permission': {'bash': {'printf *': 'allow', 'git push *': 'ask', 'rm *': 'deny'}}}
provider = {'driver': 'opencode', 'displayName': 'OpenCode', 'enabled': True,
            'config': {'binaryPath': str(a.opencode_binary.resolve())}}
if a.check:
    assert json.loads((project / 'opencode.json').read_text())['permission'] == native['permission']
    actual = json.loads(settings.read_text())['providerInstances']['opencode']
    assert actual['driver'] == 'opencode' and actual['enabled']
    assert Path(actual['config']['binaryPath']).resolve() == a.opencode_binary.resolve()
    print('Synthetic native rules and OpenCode 2 binary selection verified.')
else:
    project.mkdir(parents=True, exist_ok=True)
    subprocess.run(['git', 'init', str(project)], check=True)
    (project / 'opencode.json').write_text(json.dumps(native, indent=2) + '\n')
    settings.parent.mkdir(parents=True, exist_ok=True)
    s = json.loads(settings.read_text()) if settings.exists() else {}
    s.setdefault('providerInstances', {})['opencode'] = provider
    settings.write_text(json.dumps(s, indent=2) + '\n')
    print(project)
