#!/usr/bin/env python3
"""Create synthetic repositories once and add them via each checkout's CLI.
Usage: seed.py CHECKOUT HOME [web|mobile]. Each invocation retains a new log.
"""
import datetime, json, subprocess, sys
from pathlib import Path
root = Path('/tmp/issue-lead-33607-r1/evidence-capture-1')
checkout, home = map(Path, sys.argv[1:3])
suite = sys.argv[3] if len(sys.argv) > 3 else 'web'
run = root / ('seed-' + datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%S.%fZ'))
run.mkdir()
fixtures = [
 ('F01','my-repo','https://dev.azure.com/example-org/My%20Project/_git/my-repo',2),
 ('F02','ssh-repo','git@ssh.dev.azure.com:v3/Example-Org/Other%20Project/ssh-repo',2),
 ('F07','t3demo','https://github.com/ExampleOwner/t3demo.git',2),
 ('F14','my-repo','https://dev.azure.com/example-org/Platform/_git/_full/my-repo',2),
] if suite == 'web' else [('F08','solo-repo','https://dev.azure.com/example-org/Solo%20Project/_git/solo-repo',1)]
manifest = []
with (run/'seed.log').open('w') as log:
 def call(argv, cwd=None):
  log.write(json.dumps(list(map(str,argv)))+'\n'); log.flush()
  subprocess.run(list(map(str,argv)), cwd=cwd, stdout=log, stderr=log, check=True)
 for fixture,name,remote,count in fixtures:
  for n in range(1,count+1):
   repo = root/'repos'/fixture/str(n)/name
   if not repo.exists():
    repo.mkdir(parents=True)
    call(['git','init','-q',repo])
    call(['git','-C',repo,'remote','add','origin',remote])
    call(['git','-C',repo,'-c','user.name=Synthetic Fixture','-c','user.email=fixture@example.invalid','commit','-q','--allow-empty','-m','init'])
   call(['node',checkout/'apps/server/src/bin.ts','project','add','--base-dir',home,repo],cwd=checkout/'apps/server')
   manifest.append(dict(fixture=fixture,path=str(repo),remote=remote,title=name))
(run/'manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
print(run)
