#!/usr/bin/env python3
"""Reproduction seed used for both commits, parameterized to omit host-specific paths.
Run in a disposable checkout before starting its Replay thread. No real model is used.
Usage: python seed-gallery.py CHECKOUT OUTPUT_DIRECTORY
Then start: T3CODE_DEV_SEED=0 vp run dev --host 0.0.0.0
Then use the project's CLI to add OUTPUT_DIRECTORY as 'Gallery QA' and start a
'devReplay' / 'multi-turn' thread titled 'Settings screen QA', with message:
Publish the Settings screen QA gallery of ten screenshots.
All output is synthetic. The patch only equips Replay to call the real MCP tool.
"""
from pathlib import Path
from PIL import Image, ImageDraw
import json, sys
root, p = (Path(s).resolve() for s in sys.argv[1:])
p.mkdir(parents=True, exist_ok=True)
for i in range(1, 11):
    im = Image.new('RGB', (900, 1200), '#101820')
    d = ImageDraw.Draw(im)
    d.rounded_rectangle((36,36,864,1164), radius=32, fill=['#244762','#364261','#22534b'][i%3])
    d.text((72,80), f'SETTINGS SCREEN {i:02}', font_size=52, fill='white')
    for n,t in enumerate(['Appearance','Notifications','Privacy','Connections','Advanced']):
        y=220+n*155
        d.rounded_rectangle((70,y,830,y+110), radius=16, fill='#17232e')
        d.text((95,y+30),t,font_size=36,fill='white')
        d.ellipse((740,y+35,780,y+75),fill='#65d2a5')
    d.text((72,1080),f'QA screenshot {i:02} / 10',font_size=32,fill='#d8e5ed')
    im.save(p/f'settings-{i:02}.png')
html='''<!doctype html><html><head><meta name="viewport" content="width=device-width, initial-scale=1"><style>body{margin:0;padding:12px;font:14px system-ui;color:#e5e9ee;background:#101318}h2{font-size:22px;margin:4px 0 16px}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px}figure{margin:0;min-width:0;background:#20262e;padding:8px;border-radius:10px}img{width:100%;display:block;border-radius:5px}figcaption{padding-top:8px;overflow-wrap:anywhere}</style></head><body><h2>Settings screen QA</h2><div class="grid">'''+''.join(f'<figure><img src="{p}/settings-{i:02}.png" alt="Settings screen {i:02}"><figcaption>settings-{i:02}.png</figcaption></figure>' for i in range(1,11))+'</div></body></html>'
(p/'gallery.html').write_text(html)
args=json.dumps({'path':str(p/'gallery.html'),'title':'Settings screen QA','height':2400})
f=root/'apps/server/scripts/dev-replay-agent.ts'
s=f.read_text()
needle='        const turns = loadTurns(session.model);'
patch='''        const { spawnSync } = await import("node:child_process");
        const renderCallId = NodeCrypto.randomUUID();
        const renderArgs = ARGS;
        send({method:"session/update",params:{sessionId,update:{sessionUpdate:"tool_call",toolCallId:renderCallId,title:"mcp__t3_code__html_render",kind:"other",status:"in_progress",rawInput:renderArgs}}});
        const rendered = spawnSync(process.env.T3_ACP_MCP_NODE!, [
          ...(process.env.T3_ACP_MCP_ENTRYPOINT ? [process.env.T3_ACP_MCP_ENTRYPOINT] : []),
          "acp-mcp-call", "html_render", JSON.stringify(renderArgs)
        ], {env:{...process.env,ELECTRON_RUN_AS_NODE:"1"},encoding:"utf8"});
        send({method:"session/update",params:{sessionId,update:{sessionUpdate:"tool_call_update",toolCallId:renderCallId,status:"completed",rawOutput:JSON.parse(rendered.stdout)}}});
'''.replace('ARGS',args)
if 'const renderCallId' in s:
    raise SystemExit('Use a clean disposable checkout; Replay is already patched.')
f.write_text(s.replace(needle,patch+needle))
