#!/usr/bin/env python3
from pathlib import Path
import subprocess, json
ROOT=Path(__file__).resolve().parents[1]
QA=ROOT/'qa_screenshots'; QA.mkdir(exist_ok=True)
data=json.load(open(ROOT/'assets/data/image-admin/rc45_ai_physio_first_pass.json'))
counts=data['summary']['priorityCounts']
VIEWS={'mobile':(390,844),'ipad':(820,1180),'desktop':(1440,900)}

def html_for(name,w,h):
    mobile=name=='mobile'
    pad=14 if mobile else 28
    gap=12 if mobile else 18
    grid='grid-template-columns:1fr;' if mobile else 'grid-template-columns:1fr 1fr;'
    counts_html=''.join(f'<span class="badge">{k}: {v}</span>' for k,v in counts.items())
    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;overflow:hidden;font-family:Inter,Arial,sans-serif;background:radial-gradient(circle at 8% 0%,#174e69,#06111f 55%,#03070d);color:#effbff}}
.shell{{height:100%;padding:{pad}px;display:flex;flex-direction:column;gap:{gap}px}}.top{{display:flex;justify-content:space-between;align-items:center}}.brand{{font-weight:950;letter-spacing:.08em;font-size:{17 if mobile else 24}px}}.pill{{border:1px solid rgba(120,233,255,.42);border-radius:999px;padding:8px 12px;color:#aaf4ff;background:rgba(3,22,36,.72);font-weight:900;font-size:{10 if mobile else 13}px}}
.hero{{border:1px solid rgba(120,233,255,.22);border-radius:{20 if mobile else 30}px;background:linear-gradient(180deg,rgba(10,30,48,.72),rgba(2,8,18,.92));box-shadow:0 22px 70px rgba(0,0,0,.42);padding:{18 if mobile else 32}px}}h1{{margin:0;font-size:{29 if mobile else 48}px;line-height:1.02;letter-spacing:-.045em}}p{{margin:10px 0 0;color:#bdd3df;font-size:{12 if mobile else 17}px;line-height:1.42}}
.grid{{display:grid;{grid}gap:{gap}px;min-height:0;flex:1}}.card{{border:1px solid rgba(120,233,255,.18);border-radius:{18 if mobile else 25}px;background:rgba(5,15,29,.78);padding:{15 if mobile else 24}px;box-shadow:0 18px 55px rgba(0,0,0,.30);min-height:0}}h2{{margin:0 0 10px;font-size:{19 if mobile else 28}px}}.metric{{display:grid;grid-template-columns:1fr auto;gap:10px;align-items:center;border:1px solid rgba(255,255,255,.12);border-radius:14px;padding:{10 if mobile else 13}px;margin:10px 0;background:rgba(255,255,255,.045)}}.metric b{{font-size:{20 if mobile else 29}px;color:#76e8ff}}.warn{{color:#ffd18a}}.ok{{color:#8bffcb}}.path{{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;color:#dbfbff;background:rgba(0,0,0,.23);border-radius:8px;padding:2px 5px}}.badge{{display:inline-block;border-radius:999px;padding:7px 9px;background:rgba(120,233,255,.10);border:1px solid rgba(120,233,255,.18);font-size:{10 if mobile else 12}px;color:#c6f8ff;font-weight:800;margin:6px 6px 0 0}}.flow{{display:grid;grid-template-columns:repeat(5,1fr);gap:8px;margin-top:14px}}.flow div{{border:1px solid rgba(255,255,255,.11);border-radius:14px;padding:{8 if mobile else 12}px;text-align:center;color:#d7f6ff;font-weight:850;font-size:{9 if mobile else 13}px;background:rgba(255,255,255,.045)}}.footer{{display:{'none' if mobile else 'flex'};justify-content:space-between;color:#88a4b4;font-size:12px}}
</style></head><body><main class="shell"><div class="top"><div class="brand">NEXT ATHLETE OS</div><div class="pill">RC45 · {name} QA</div></div>
<section class="hero"><h1>AI physio first-pass movement audit</h1><p>Fysion denna vecka är vår rörelse-QA: varje övning har fått purpose, must-show, avoid, ankare och appbeslut. Runtime är fortfarande orörd.</p><div class="flow"><div>Exercise truth</div><div>Skeleton standard</div><div>Candidate score warning</div><div>Review priority</div><div>Selective activation later</div></div></section>
<section class="grid"><div class="card"><h2>Audit dataset</h2><div class="metric"><span>Exercises audited</span><b>{data['summary']['exerciseCount']}</b></div><div class="metric"><span>Keyframes covered</span><b>{data['summary']['keyframeCount']}</b></div><div class="metric"><span>Runtime changed</span><b class="ok">0</b></div><div class="metric"><span>Special-handling groups</span><b class="warn">{counts.get('special-handling',0)+counts.get('special-prop-review',0)}</b></div><p>Tool: <span class="path">tools/physio_first_pass_lab.html</span></p></div>
<div class="card"><h2>Review priorities</h2><div>{counts_html}</div><p>High score is not approval. It only means the candidate matched the old seed skeleton. RC45 tells us which movement truth to enforce before activation.</p><p>Doc: <span class="path">docs/RC45_AI_PHYSIO_FIRST_PASS.md</span></p></div></section><div class="footer"><span>Static fallback render: WeasyPrint → PNG</span><span>Chromium/Playwright not used</span></div></main></body></html>'''

for name,(w,h) in VIEWS.items():
    html=ROOT/f'qa_rc45_{name}.html'; pdf=QA/f'rc45_{name}_static_qa.pdf'; prefix=QA/f'rc45_{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'rc45_{name}_static_qa.png')
