# v3 final renders: one Chromium, per-card / per-list shots into raw/, then labelled contact sheets in out-v3/.
import sys,os,shutil
sys.path.insert(0,os.path.abspath(os.path.join(os.path.dirname(__file__),'..','..')))
import render2
from PIL import Image,ImageDraw,ImageFont
from playwright.sync_api import sync_playwright
OUT=os.path.abspath(os.path.join(os.path.dirname(__file__),'..'));RAW=os.path.join(OUT,'raw')
shutil.rmtree(RAW,ignore_errors=True);os.makedirs(RAW)
FB=lambda s:ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',s);FN=lambda s:ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',s)
THEMES=('midnight','pearl','champagne','amethyst')
OPT=['go-vfr','nogo-ifr','night-2a','heliport-night']
LVN={'a':'A · lighting token + VFR min on eyebrow (0 px)','b':'B · 4th pill: minimum | table | lighting (+36 px)','c':'C · i popover overlay (0 px on card)','d':'D · same facts in flow under the chip row (card grows)'}
def load(pg,q):
  pg.goto('https://mock.local/MOCK/out-v3/src/final-v3.html?'+q);pg.wait_for_timeout(1300)
  pg.evaluate("document.getAnimations().forEach(a=>{try{a.finish()}catch(e){a.cancel()}})");pg.wait_for_timeout(120)
def cards(pg,q,tag):
  load(pg,q);r=[]
  for e in pg.query_selector_all('#list .card'):
    sc=e.get_attribute('data-mk-sc') or e.query_selector('.icao').text_content().strip()
    f=os.path.join(RAW,f'{tag}_{sc}.png');e.screenshot(path=f,animations='disabled');r.append((sc,f))
  return r
def lst(pg,q,tag):
  load(pg,q);f=os.path.join(RAW,tag+'.png');pg.query_selector('#list').screenshot(path=f,animations='disabled');return f
def sheet(name,title,sub,rows,cols=6,scale=.5):
  # rows: list of (label, [(caption,file)])
  ims=[[(c,Image.open(f).convert('RGB')) for c,f in it] for _,it in rows]
  ims=[[(c,i.resize((int(i.width*scale),int(i.height*scale)),Image.LANCZOS)) for c,i in r] for r in ims]
  cw=max(i.width for r in ims for _,i in r);G=24;cap=34
  W=max(min(cols,len(r)) for r in ims)*(cw+G)+G+40
  blocks=[]
  for (lab,_),r in zip(rows,ims):
    lines=[r[k:k+cols] for k in range(0,len(r),cols)]
    blocks.append((lab,lines,sum(max(i.height for _,i in l)+cap+G for l in lines)))
  H=150+sum(b[2]+(60 if b[0] else 0) for b in blocks)
  S=Image.new('RGB',(W,H),(12,12,15));d=ImageDraw.Draw(S)
  d.text((32,26),title,font=FB(34),fill=(255,255,255));d.text((32,82),sub,font=FN(19),fill=(170,170,182));y=140
  for lab,lines,h in blocks:
    if lab:d.text((32,y+8),lab,font=FB(26),fill=(255,214,120));y+=60
    for l in lines:
      x=32
      for c,i in l:d.text((x,y),c,font=FN(20),fill=(205,205,215));S.paste(i,(x,y+cap));x+=cw+G
      y+=max(i.height for _,i in l)+cap+G
  p=os.path.join(OUT,name);S.save(p,optimize=True);print(name,S.size);return p
SUB='Go Around aerodrome card v3 MOCKUP · live/model-3h-11nov markup + v3.css / v3.js at source · 13 Nov 2026 · mockup only, no repo touched'
with sync_playwright() as p:
  br=p.chromium.launch()
  try:
    c=render2.mkctx(br,390);pg=c.new_page()
    for th in THEMES:
      r=cards(pg,f'theme={th}&board=states',f'390_{th}_states')
      sheet(f'SHEET_390_states_{th}.png',f'iPhone 390 · {th.title()} · all weather states (Day + after-sunset night-2a)',SUB,[('',r)],cols=6)
    for th in ('midnight','pearl'):
      r=cards(pg,f'theme={th}&at=17:00&phase=land&eta=2',f'390_{th}_land')
      sheet(f'SHEET_390_after-sunset-land_{th}.png',f'iPhone 390 · {th.title()} · real board, LAND phase, ETA 19:00 (after sunset)',SUB,[('',r)],cols=5)
    for th in ('midnight','pearl'):
      rows=[]
      for lv in 'abcd':rows.append((LVN[lv],cards(pg,f'theme={th}&board=states&lv={lv}&sc='+','.join(OPT),f'390_{th}_lv{lv}')))
      sheet(f'SHEET_390_lighting-vfrmin_options-A-D_{th}.png',f'Lighting & VFR minimums · Options A–D · iPhone 390 · {th.title()}',SUB,rows,cols=4)
    c.close()
    c=render2.mkctx(br,1440);pg=c.new_page()
    for th in THEMES:
      f=lst(pg,f'theme={th}&board=states',f'1440_{th}_states')
      sheet(f'SHEET_1440_states_{th}.png',f'Desktop 1440 · {th.title()} · all states, equal-height 3-card rows',SUB,[('',[('#list',f)])],cols=1,scale=.6)
    f=lst(pg,'theme=midnight&at=17:00&phase=land&eta=2','1440_midnight_land')
    sheet('SHEET_1440_after-sunset-land_midnight.png','Desktop 1440 · Midnight · real board, LAND phase after sunset',SUB,[('',[('#list',f)])],cols=1,scale=.6)
    rows=[(LVN[lv],[('#list · go-vfr / night-2a / heliport-night',lst(pg,f'theme=midnight&board=states&lv={lv}&sc=go-vfr,night-2a,heliport-night',f'1440_lv{lv}'))]) for lv in 'abcd']
    sheet('SHEET_1440_lighting-vfrmin_options-A-D_midnight.png','Lighting & VFR minimums · Options A–D · Desktop 1440 · Midnight (one 3-card row each)',SUB,rows,cols=1,scale=.6)
    c.close()
  finally:br.close()
