#!/usr/bin/env python3
"""Synthetic provider setup for the real app; no database rows are inserted.
Usage: seed.py CHECKOUT HOME [--configure | --start | --queue THREAD]
Start the server with T3CODE_DEV_SEED=0 before --start.
Browser sequence: record; release A; select Account B / Default; send
'Send immediately on Account B'; retain the pre-reset state; after reset
send 'Account B after the reported reset' on B and retain the final state.
"""
import json,pathlib,subprocess,sys,time
root=pathlib.Path(__file__).resolve().parent
checkout=pathlib.Path(sys.argv[1]);home=pathlib.Path(sys.argv[2]);action=sys.argv[3]
def cli(*args):
 r=subprocess.run(['node','apps/server/src/bin.ts',*args,'--base-dir',str(home)],cwd=checkout,text=True,capture_output=True,check=True)
 return r.stdout
if action=='--configure':
 state=home/'userdata';state.mkdir(parents=True,exist_ok=True)
 instances={k:{'driver':k,'enabled':False} for k in ['codex','claudeAgent','cursor','grok','muse','pi','opencode','antigravity']}
 for k in ['a','b']:
  account=root/('home-'+k);account.mkdir(exist_ok=True)
  instances['account'+k.upper()]={'driver':'codex','displayName':'Account '+k.upper(),'enabled':True,'config':{'binaryPath':str(root/'provider.cjs'),'homePath':str(account),'customModels':[{'slug':'default','name':'Default'}]}}
 (state/'settings.json').write_text(json.dumps({'defaultModelSelection':{'instanceId':'accountA','model':'default'},'providerInstances':instances}))
elif action=='--start':
 (root/'control.json').write_text(json.dumps({'resetAt':int(time.time())+120}))
 subprocess.run(['node','apps/server/src/bin.ts','project','add',str(root/'demo'),'--title','Provider queue proof','--base-dir',str(home)],cwd=checkout,check=False,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
 result=json.loads(cli('thread','start',str(root/'demo'),'Start on Account A','--title','Provider queue isolation','--provider','accountA','--model','default','--json'))
 print(json.dumps(result))
elif action=='--queue':
 print(cli('thread','send',sys.argv[4],'Queued for Account A: preserve my saved account','--json'))
elif action=='--release':
 (root/'release-a').touch()
