from pathlib import Path
from PIL import Image, ImageChops, ImageDraw, ImageFont
import json,sys
root=Path(sys.argv[1]);side=sys.argv[2];sha=sys.argv[3]
font='/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'
f=lambda n:ImageFont.truetype(font,n)
summary=json.load(open(root/'summary.json'))
public=[]
for case in summary:
 label=f"{case['received']}-of-{case['total']}"
 a=Image.open(root/(label+'-first.png')).convert('RGB');b=Image.open(root/(label+'.png')).convert('RGB')
 a=a.crop((0,48,a.width,a.height));b=b.crop((0,48,b.width,b.height))
 stable=ImageChops.difference(a,b).getbbox() is None
 if not stable:raise Exception('App pixels are not stable: '+label)
 b.save(root/(label+'-app.png'))
 ax=case['progress'][0]
 safe={k:v for k,v in ax.items() if k not in ['pid','ref']}
 public.append({'receivedBytes':case['received'],'totalBytes':case['total'],'accessibility':safe,'appPixelsIdenticalAcrossCaptures':stable})
 panel=Image.new('RGB',(402,1040),'#f8fafc');panel.paste(b,(0,62));d=ImageDraw.Draw(panel)
 d.text((16,10),f'{side}  {sha[:10]}',font=f(22),fill='#111827')
 d.text((16,38),'iPhone 18 Pro · iOS 27.2 · Light',font=f(14),fill='#475569')
 d.rectangle((0,888,402,1040),fill='#111827')
 d.text((16,902),'Native accessibility snapshot',font=f(18),fill='white')
 d.text((16,934),'ZIP download progress',font=f(16),fill='#cbd5e1')
 d.text((16,961),'value: '+ax['value'],font=f(28),fill='#ffffff')
 d.text((16,1004),f"{case['received']:,} / {case['total']:,} bytes received",font=f(16),fill='#cbd5e1')
 panel.save(root/(label+'-annotated.png'))
json.dump({'commit':sha,'device':'iPhone 18 Pro','os':'iOS 27.2','appearance':'Light','appViewport':'402x826; OS status bar cropped','cases':public},open(root/'accessibility-evidence.json','w'),indent=2)
