from PIL import Image, ImageDraw, ImageFont
from pathlib import Path
import json
root=Path(__file__).parent
font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',18)
for side in ['before','after']:
    out=root/(side+'-1')
    lines=(out/'transcript.txt').read_text().splitlines()
    canvas=Image.new('RGB',(1530,670),'#10151e')
    draw=ImageDraw.Draw(canvas)
    for i,line in enumerate(lines):
        color='#e5e9f0'
        if 'code2Interrupted' in line: color='#ffb4a8' if side=='before' else '#9fe3b0'
        draw.text((24,22+i*28),line,font=font,fill=color)
    canvas.save(out/'capture.png')
    records=[]
    for line in (root/('base-data' if side=='before' else 'head-data')/'userdata/logs/server.trace.ndjson').read_text().splitlines():
        try:
            record=json.loads(line)
            if str(record.get('name','')).startswith('repro.'):
                records.append(record)
        except ValueError: pass
    (out/'synthetic-traces.ndjson').write_text(''.join(json.dumps(r)+'\n' for r in records))
    print(side,len(records))
