from PIL import Image, ImageDraw, ImageFont
import pathlib, json, textwrap
root=pathlib.Path(__file__).parent
font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',18)
for side in ['before','after']:
    run=sorted(root.glob(side+'-*'))[-1]
    data=json.loads((run/'observations.json').read_text())
    raw=(run/'capture.txt').read_text()
    lines=['T3 CLI / stored tracked-folder labels',side.upper()+' | '+data['sha'],'Linux x86_64 | Node v26.13.1 | fresh state per run','Captured real CLI + SQLite output; paths redacted.','ASCII JSON escapes make surrogate boundaries visible.','']
    for number, r in enumerate(data['records']):
        label=['127 ASCII a + rocket','126 ASCII b + rocket','127 spaces + rocket'][number]
        lines+=['INPUT: '+label,'COMMAND: t3 project add app --track <folder> --base-dir state','CLI exit: 0; project added successfully.','SQLite projection_projects.tracked_folders_json -> name:']
        lines+=textwrap.wrap(json.dumps(r['storedName'],ensure_ascii=True),100,replace_whitespace=False,drop_whitespace=False)
        lines += ['UTF-16 units: '+str(r['utf16Units'])+' | last unit: '+r['lastUnit'],'Well formed: '+str(r['wellFormed']).lower(),'']
    lines+=['Full output and reproducible driver supplied separately.']
    image=Image.new('RGB',(1200,70+len(lines)*29),'#111827'); draw=ImageDraw.Draw(image)
    for i,line in enumerate(lines):
        color='#e5e7eb'
        if line.startswith('Well formed:'): color='#86efac' if line.endswith('true') else '#fca5a5'
        draw.text((32,28+i*29),line,font=font,fill=color)
    image.save(root/(side+'.png'))
    print(side, data)
