import sys, pathlib, subprocess, wave, math, struct, os
checkout,home,port=sys.argv[1:]
workspace=pathlib.Path(home)/'workspace'/'Preview sample'
for folder in ['notes','notes#old','notes?old','docs','docs#old','docs?old']:
 (workspace/folder).mkdir(parents=True,exist_ok=True)
for name in ['notes/clip.wav','notes/clip#1.wav','notes/clip?1.wav','notes#old/clip.wav','notes?old/clip.wav']:
 with wave.open(str(workspace/name),'wb') as f:
  f.setparams((1,2,8000,16000,'NONE','not compressed'))
  f.writeframes(b''.join(struct.pack('<h',int(3000*math.sin(2*math.pi*440*i/8000))) for i in range(16000)))
for name in ['docs/draft.md','docs/draft#1.md','docs/draft?1.md','docs#old/draft.md','docs?old/draft.md']:
 (workspace/name).write_text('# Preview sample\n\nRendered **Markdown** content.\n')
for args in [['init','--quiet','--initial-branch=main'],['add','.'],['-c','user.name=Sample','-c','user.email=sample@example.invalid','commit','--quiet','-m','Sample files']]:
 subprocess.run(['git',*args],cwd=workspace,check=True)
env={**os.environ,'T3CODE_PORT':port}
def cli(args):
 subprocess.run(['node','apps/server/src/bin.ts',*args,'--base-dir',home],cwd=checkout,env=env,check=True,stdout=subprocess.DEVNULL)
cli(['project','add',str(workspace),'--title','Preview sample'])
cli(['thread','start',str(workspace),'Ready','--title','File preview check','--provider','devReplay','--model','multi-turn','--wait','--json'])
