from PIL import Image, ImageDraw, ImageFont
from pathlib import Path
import textwrap
root=Path(__file__).parent
for side in ['base','head']:
    raw=(root/side/'transcript.txt').read_text()
    lines=raw.splitlines()
    selected=lines[:4]+['','[Command output excerpts; full transcript attached]','']
    for i,line in enumerate(lines):
        if line.startswith('$ vp run') or line.startswith('> vp i18n') or line.startswith('check:changed summary') or line.startswith('  pass ') or line.startswith('  FAIL ') or line.startswith('Exit status:') or line.startswith('apps/web:') or 'ERROR: gt Error:' in line or line.startswith('Run `vp run i18n:generate`'):
            selected += textwrap.wrap(line, width=108, replace_whitespace=False, drop_whitespace=False) or ['']
            if line.startswith('Exit status:'): selected+=['']
    font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',18)
    img=Image.new('RGB',(1280,max(500,90+len(selected)*28)), '#10151d')
    d=ImageDraw.Draw(img)
    d.text((32,22),'BEFORE — BASE' if side=='base' else 'AFTER — FIX',font=font,fill='#72c7ff')
    for i,line in enumerate(selected):
        d.text((32,68+i*28),line,font=font,fill='#ffb870' if 'FAIL' in line or 'ERROR:' in line else '#e5e9f0')
    img.save(root/(side+'.png'))
