"""Fresh reproduction setup. Run once per side with its own checkout and scratch root.
Usage: python3 seed.py CHECKOUT SCRATCH OPENCODE_2_BINARY PROVIDER_CONFIG
PROVIDER_CONFIG is a local OpenCode config containing the live model credentials.
Credentials are never embedded in this script or evidence.
Start the checkout with T3CODE_DEV_SEED=0 vp run dev after setup.
"""
import json, pathlib, subprocess, sys
checkout, scratch, binary, source = map(pathlib.Path, sys.argv[1:])
scratch.mkdir(parents=True, exist_ok=True)
for name in ["config", "data", "state", "cache"]:
    (scratch / name).mkdir(exist_ok=True)
config = json.loads(source.read_text())
config.pop("mcp", None)
config.pop("tools", None)
config_dir = scratch / "config" / "opencode"
config_dir.mkdir(exist_ok=True)
(config_dir / "opencode.json").write_text(json.dumps(config))
wrapper = scratch / "opencode"
wrapper.write_text("#!/bin/sh\n" + "".join(
    f'export XDG_{name.upper()}_HOME="{scratch / name}"\n'
    for name in ["config", "data", "state", "cache"]
) + f'exec "{binary}" "$@"\n')
wrapper.chmod(0o700)
settings = checkout / ".t3" / "userdata"
settings.mkdir(parents=True, exist_ok=True)
(settings / "settings.json").write_text(json.dumps({
    "providerInstances": {"opencode": {"driver": "opencode", "enabled": True,
        "config": {"enabled": True, "binaryPath": str(wrapper)}}},
    "textGenerationModelSelection": {"instanceId": "opencode",
        "model": "anthropic/claude-haiku-5-5", "options": []}
}))
project = scratch / "color-demo"
project.mkdir(exist_ok=True)
(project / "README.md").write_text("# Color demo\n")
for args in [["init", "-q", "-b", "main"], ["add", "."],
    ["-c", "user.name=Demo", "-c", "user.email=demo@example.invalid", "commit", "-qm", "Initial commit"]]:
    subprocess.run(["git", *args], cwd=project, check=True)
print("After starting dev, add the project with:")
print("node apps/server/src/bin.ts project add", project, "--title 'Color demo' --base-dir .t3")
print("Remove the automatically bootstrapped server project with project remove --force before capture.")
