#!/usr/bin/env python3
"""Render measured systemd transcript as a time-lapse, preserving real elapsed labels.
Does not synthesize measurements. Omits the unused /api/health probe (not an app endpoint).
"""
import json,pathlib,subprocess,sys
root=pathlib.Path(sys.argv[1])
for side in ['before','after']:
    d=root/side
    events=json.loads((d/'observations.json').read_text())
    sha=(d/'transcript.txt').read_text().split('Commit: ')[1].splitlines()[0]
    frames=d/'public-frames'; frames.mkdir(exist_ok=True)
    history=[]
    for i,event in enumerate(events,1):
        history.append(event)
        lines=['BOOT SERVICE LOG / CAPTURED SYSTEMD MEASUREMENTS',side.upper()+'  '+sha[:10],'Linux | Node 26 | systemd user service | fresh synthetic data','','Seed 17 MiB -> start -> wait -> restart -> append 17 MiB live.','Observe for 11 minutes without restarting.','Time-lapse: one frame per measured sample; actual elapsed time below.','','Elapsed: '+str(event['seconds'])+' s    '+event['stage'],'Service: '+event['service'],'boot-service.log: '+format(event['logBytes'],',')+' bytes','Original inode retained: '+str(event['sameInode']),'boot-service.log.1: '+(format(event['archiveBytes'],',')+' bytes' if event['archiveBytes'] is not None else 'absent'),'','Recent measured observations:']
        for e in history[-7:]: lines.append(f"{e['seconds']:>6}s  {e['stage']:<23} log={e['logBytes']:>10} archive={str(e['archiveBytes']):>10}")
        subprocess.run(['convert','-size','1440x900','xc:#101820','-font','DejaVu-Sans-Mono','-pointsize','22','-fill','#f2f5f7','-gravity','NorthWest','-annotate','+32+32','\n'.join(lines),str(frames/f'{i:04}.png')],check=True)
    subprocess.run(['ffmpeg','-y','-loglevel','error','-framerate','2','-i',str(frames/'%04d.png'),'-c:v','libx264','-pix_fmt','yuv420p','-crf','20',str(d/'evidence.mp4')],check=True)
    # Final running observation, before intentional shutdown.
    subprocess.run(['cp',str(frames/f'{len(events)-1:04}.png'),str(d/'evidence.png')],check=True)
