"""Synthetic fixtures for the manual proof; never reads a user's database.
Usage: python3 fixture-seed.py prepare|restore|inspect CHECKOUT RUN_DIRECTORY
Use a fresh RUN_DIRECTORY for each commit. Run vp i before starting the app.
"""
import pathlib, subprocess, json, sys
mode, checkout_arg, run_arg = sys.argv[1:]
checkout=pathlib.Path(checkout_arg).resolve()
run=pathlib.Path(run_arg).resolve();run.mkdir(parents=True,exist_ok=True)
repo=run/'demo-project'
external=run/'custom-worktrees/external-worktree'
local=checkout/'.t3/worktrees/local-control'
def git(*args):
    subprocess.run(['git',*args],cwd=repo,check=True,stdout=subprocess.DEVNULL)
if mode=='prepare':
    repo.mkdir()
    (repo/'README.md').write_text('Disposable cleanup demonstration\n')
    (repo/'.gitignore').write_text('ignored-marker.txt\n')
    git('init','-q','-b','main');git('add','.')
    git('-c','user.name=Demo','-c','user.email=demo@example.invalid','commit','-qm','Initial')
    for branch,path in [('external-worktree',external),('local-control',local)]:
        git('worktree','add','-q','-b',branch,str(path))
        (path/'ignored-marker.txt').write_text('Keep this ignored file\n')
    subprocess.run(['node','apps/server/src/bin.ts','project','add',str(repo),
                    '--title','Cleanup demo','--base-dir',str(checkout/'.t3')],cwd=checkout,check=True)
elif mode=='restore':
    for branch,path in [('external-worktree',external),('local-control',local)]:
        if not path.exists():
            git('worktree','add','-q',str(path),branch)
            (path/'ignored-marker.txt').write_text('Keep this ignored file\n')
elif mode=='inspect':
    print(json.dumps({name:{'checkoutExists':path.exists(),
        'ignoredMarkerExists':(path/'ignored-marker.txt').exists()}
        for name,path in [('external',external),('local',local)]},indent=2))
else:
    raise SystemExit('Unknown mode')
