"""Create synthetic data and pair/open Nitro after starting the isolated dev backend.
Usage: python setup-proof.py CHECKOUT DATA_DIR WORKSPACE BACKEND_ORIGIN SESSION
Start server with T3CODE_DEV_SEED=0 and T3CODE_TERMINAL_SHELL pointing to startup-shell.
Install cached Nitro dev client and launch its JS server separately, per mobile-nitro README.
Run vp exec nitro-ui prepare in apps/mobile-nitro before starting its JS server.
The startup reader remains alive after EOF so Nitro keeps its terminal result visible.
"""
import subprocess,sys,json,re,urllib.parse
from pathlib import Path
checkout,data,workspace,origin,session=sys.argv[1:]
def cli(*args):
 return subprocess.run(["node","apps/server/src/bin.ts",*args,"--base-dir",data],cwd=checkout,capture_output=True,text=True,check=True).stdout
cli("project","add",workspace,"--title","Startup paste proof")
thread=json.loads(cli("thread","start",workspace,"Ready for terminal proof.","--title","Startup paste proof","--provider","devReplay","--model","showcase","--json"))["threadId"]
pair=cli("auth","pairing","create","--base-url",origin,"--ttl","15m","--label","proof")
url=re.search(r"Pair URL:\s*(\S+)",pair).group(1)
link="t3nitro-dev://connections/new?"+urllib.parse.urlencode({"pairingUrl":url,"autoConnect":"1"})
subprocess.run(["agent-device","open","com.t3tools.nitrotrial.dev",link,"--session",session],stdout=subprocess.DEVNULL,check=True)
print("Tap Add environment, then open this thread using the app link:")
env=(Path(data)/"userdata"/"environment-id").read_text().strip()
print("t3nitro-dev://threads/"+urllib.parse.quote(env,safe="")+"/"+urllib.parse.quote(thread,safe=""))
print("Tap Terminal, Open new terminal. Then run drive-proof.py against that session.")
