#!/usr/bin/env python3
from pathlib import Path
import subprocess
ROOT=Path(__file__).resolve().parents[1]
QA=ROOT/'qa_screenshots'; QA.mkdir(exist_ok=True)
VIEWS={'mobile':(390,844),'ipad':(820,1180),'desktop':(1440,900)}

def html_for(name,w,h):
    is_mobile=name=='mobile'
    img1='qa_screenshots/rc39_sequence_anchor_trace_lateral_line_hops.jpg'
    img2='qa_screenshots/rc39_limb_binding_contact_map.jpg'
    stage_h=int(h*.43 if is_mobile else h*.58)
    cards='grid-template-columns:1fr;' if is_mobile else 'grid-template-columns:1.15fr .85fr;'
    return f'''<!doctype html><html><head><meta charset="utf-8"><style>
@page {{ size:{w}px {h}px; margin:0; }}
*{{box-sizing:border-box}} body{{margin:0;width:{w}px;height:{h}px;background:radial-gradient(circle at 8% 0%,#154963,#071220 54%,#040811);font-family:Inter,Arial,sans-serif;color:#effbff;overflow:hidden}}
.shell{{height:100%;padding:{16 if is_mobile else 28}px;display:flex;flex-direction:column;gap:{12 if is_mobile else 18}px}}
.top{{display:flex;align-items:center;justify-content:space-between;gap:14px}} .brand{{font-weight:950;letter-spacing:.09em;font-size:{17 if is_mobile else 24}px}} .pill{{padding:8px 12px;border:1px solid rgba(120,233,255,.42);border-radius:999px;background:rgba(3,22,36,.72);color:#aaf4ff;font-weight:900;font-size:{10 if is_mobile else 13}px}}
.grid{{display:grid;{cards}gap:{12 if is_mobile else 18}px;min-height:0;flex:1}}
.stage{{position:relative;min-height:{stage_h}px;border:1px solid rgba(120,233,255,.22);border-radius:{20 if is_mobile else 30}px;background:linear-gradient(180deg,rgba(10,30,48,.72),rgba(2,8,18,.92));overflow:hidden;box-shadow:0 22px 70px rgba(0,0,0,.42)}}
.stage img{{position:absolute;left:0;top:0;width:100%;height:100%;object-fit:contain;padding:{8 if is_mobile else 16}px}}
.panel{{border:1px solid rgba(120,233,255,.18);border-radius:{18 if is_mobile else 25}px;background:rgba(5,15,29,.78);padding:{15 if is_mobile else 24}px;box-shadow:0 18px 55px rgba(0,0,0,.30);display:flex;flex-direction:column;justify-content:space-between;gap:14px}}
h1{{margin:0;font-size:{24 if is_mobile else 40}px;line-height:1.0;letter-spacing:-.04em}} p{{margin:10px 0 0;color:#b9cfdb;font-size:{12 if is_mobile else 16}px;line-height:1.42}}
.check{{display:flex;gap:10px;margin-top:12px;color:#e7fcff;font-size:{12 if is_mobile else 15}px;line-height:1.32}} .check b{{color:#75eaff}}
.preview{{height:{155 if is_mobile else 260}px;border-radius:18px;overflow:hidden;border:1px solid rgba(120,233,255,.18);background:#020811}} .preview img{{width:100%;height:100%;object-fit:cover}}
.badges{{display:flex;flex-wrap:wrap;gap:7px;margin-top:14px}} .badge{{border-radius:999px;padding:7px 9px;background:rgba(120,233,255,.10);border:1px solid rgba(120,233,255,.18);font-size:{10 if is_mobile else 12}px;color:#c6f8ff;font-weight:800}}
.footer{{display:flex;justify-content:space-between;gap:8px;color:#88a4b4;font-size:{9 if is_mobile else 12}px}}
</style></head><body><main class="shell"><div class="top"><div class="brand">NEXT ATHLETE OS</div><div class="pill">RC39 · {name} QA</div></div><section class="grid"><div class="stage"><img src="{img1}"></div><aside class="panel"><div><h1>Limb intent + MediaPipe bridge</h1><p>Fot/handskelett är nu kopplat till namngivna landmarks per sprite och per rörelsefas. MediaPipe-fält finns för nästa extractor-pass utan ny animationsmotor.</p><div class="check"><b>✓</b><span>136 sekvensposer har explicit <b>limbIntent</b> och <b>anchorLandmark</b>.</span></div><div class="check"><b>✓</b><span>288 sprites har v2-runtime landmarks inklusive <b>dominantContactFoot</b>.</span></div><div class="check"><b>✓</b><span>Runtime läser sekvensankare först, därefter kontakt-aware fallback.</span></div><div class="badges"><span class="badge">no random toe</span><span class="badge">same sprite/skeleton flip</span><span class="badge">MediaPipe-ready</span></div></div><div class="preview"><img src="{img2}"></div></aside></section><div class="footer"><span>Static fallback render: WeasyPrint → PNG</span><span>Chromium/Playwright unavailable in sandbox</span></div></main></body></html>'''

for name,(w,h) in VIEWS.items():
    html=ROOT/f'qa_rc39_{name}.html'; pdf=QA/f'rc39_{name}_static_qa.pdf'; prefix=QA/f'rc39_{name}_static_qa'
    html.write_text(html_for(name,w,h),encoding='utf-8')
    subprocess.run(['weasyprint',str(html),str(pdf)],check=True,cwd=ROOT)
    subprocess.run(['pdftoppm','-png','-singlefile','-r','96',str(pdf),str(prefix)],check=True)
    print(QA/f'rc39_{name}_static_qa.png')
