import json, re, io, base64, html
from PIL import Image
exec(open('plan.py').read())
B=json.load(open('beats.json')); pages=json.load(open('pages.json'))
bmap={b['id']:b for b in B}
TH={}
def thumb(k):
    if k in TH or k is None: return
    im=Image.open(f't/thumbs/{k}.jpg').convert('RGB').resize((320,180),Image.LANCZOS)
    b=io.BytesIO(); im.save(b,'JPEG',quality=72); TH[k]='data:image/jpeg;base64,'+base64.b64encode(b.getvalue()).decode()
rows=[]
DEC=json.load(open('decisions_backup/v2/all.json'))
CUT={'024_S06_04_a_Empty_games','037_S08_06_Taken','010b_S03_02_b_She_runs','007b_S02_03_b_Bats'}
for st,sid,dur,bid in [p for p in pages if p[0] not in CUT]:
    b=bmap[bid]; p=P[st]
    thumb(st); thumb(p['endRef']); thumb(p['startRef'])
    d=float(dur); clip=0 if p['end']=='NONE' else max(5,round(d+0.49))
    paint=(1 if p['end'] in ('NUDGE','NEW') else 0)+(1 if p['start']=='ends' and not p.get('startRef') else 0)
    rows.append(dict(id=st,shot=sid,title=b['title'],seq=b['seq'],seqname=b['seqname'],seqtime=b['seqtime'].split('|')[0].strip(),dur=d,clip=clip,
        action=b.get('Action and emotion',''),paint=paint,applied=bool((DEC.get(st) or {}).get('note')),**p))
import os
FULL=sorted(f[:-4] for f in os.listdir('full') if f.endswith('.jpg'))
SRC={k:v for k,v in json.load(open('sources.json')).items()}
data=json.dumps(dict(rows=rows,thumbs=TH,full=FULL,src=SRC))
tpl=open('board_tpl.html').read()
open('/home/claude/triage/zula_end_frame_board.html','w').write(tpl.replace('/*DATA*/null',data))
from collections import Counter
print(Counter(r['end'] for r in rows), Counter(r['start'] for r in rows), sum(r['paint'] for r in rows), sum(r['clip'] for r in rows))
