#!/usr/bin/env python3
from pathlib import Path
import subprocess, textwrap
ROOT=Path(__file__).resolve().parents[1]
QA=ROOT/'qa_screenshots'; QA.mkdir(exist_ok=True)
ASSETS=ROOT/'assets/img/exercises/pose-atlas/sprites/athlete-b'

VIEWS={
  'mobile':(390,844),
  'ipad':(820,1180),
  'desktop':(1440,900),
}

def html_for(name,w,h):
    stage_h = int(h*0.46 if name!='desktop' else h*0.58)
    img1='assets/img/exercises/pose-atlas/sprites/athlete-b/sheet_06_footwork_shuffle__pose05.png'
    img2='assets/img/exercises/pose-atlas/sprites/athlete-b/sheet_06_footwork_shuffle__pose08.png'
    img3='assets/img/exercises/pose-atlas/sprites/athlete-b/sheet_10_medball_throw_slam__pose07.png'
    is_mobile=name=='mobile'
    cards_css = 'grid-template-columns:1fr;' if is_mobile else 'grid-template-columns:1.2fr .8fr;'
    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 10% 0%,#164864,#07111d 52%,#040810);font-family:Inter,Arial,sans-serif;color:#eff9ff;overflow:hidden}}
.shell{{padding:{18 if is_mobile else 28}px;display:flex;flex-direction:column;gap:{14 if is_mobile else 18}px;height:100%}}
.top{{display:flex;align-items:center;justify-content:space-between;gap:16px}} .brand{{font-weight:900;letter-spacing:.08em;font-size:{18 if is_mobile else 24}px}} .pill{{padding:8px 12px;border:1px solid rgba(100,225,255,.45);border-radius:999px;background:rgba(3,22,36,.7);color:#9befff;font-size:{11 if is_mobile else 13}px}}
.grid{{display:grid;{cards_css};gap:{14 if is_mobile else 18}px;min-height:0;flex:1}}
.stage{{position:relative;min-height:{stage_h}px;border:1px solid rgba(122,233,255,.22);border-radius:{22 if is_mobile else 30}px;background:linear-gradient(180deg,rgba(11,28,46,.7),rgba(2,8,18,.92));box-shadow:0 20px 70px rgba(0,0,0,.42);overflow:hidden}}
.floor{{position:absolute;left:6%;right:6%;bottom:18%;height:2px;background:linear-gradient(90deg,transparent,#79eaff,transparent);opacity:.55}}
.sprite{{position:absolute;bottom:18%;height:{200 if is_mobile else 320}px;object-fit:contain;filter:drop-shadow(0 25px 28px rgba(0,0,0,.45))}}
.s1{{left:{55 if is_mobile else 160}px;height:{170 if is_mobile else 260}px}} .s2{{left:{150 if is_mobile else 390}px;height:{176 if is_mobile else 270}px}} .s3{{right:{55 if is_mobile else 180}px;height:{170 if is_mobile else 260}px}}
.skel{{position:absolute;left:50%;top:18%;width:{130 if is_mobile else 210}px;height:{220 if is_mobile else 340}px;border-radius:30px;border:1px dashed rgba(93,233,255,.28);opacity:.85}}
.dot{{position:absolute;width:10px;height:10px;border-radius:50%;background:#fff;box-shadow:0 0 0 4px rgba(87,229,255,.25)}} .d1{{left:26%;bottom:18%}} .d2{{left:49%;bottom:18%}} .d3{{right:27%;bottom:18%}}
.panel{{border:1px solid rgba(122,233,255,.18);border-radius:{18 if is_mobile else 24}px;background:rgba(5,15,28,.78);padding:{16 if is_mobile else 22}px;box-shadow:0 18px 55px rgba(0,0,0,.28)}}
h1{{margin:0 0 10px;font-size:{24 if is_mobile else 38}px;line-height:1.02}} p{{margin:0;color:#b8cdda;font-size:{13 if is_mobile else 16}px;line-height:1.45}} .check{{display:flex;gap:10px;align-items:flex-start;margin-top:14px;color:#dffaff;font-size:{13 if is_mobile else 16}px}} .check b{{color:#78e9ff}}
.badges{{display:flex;flex-wrap:wrap;gap:8px;margin-top:16px}} .badge{{padding:7px 10px;border-radius:999px;background:rgba(120,233,255,.10);border:1px solid rgba(120,233,255,.20);font-size:{11 if is_mobile else 13}px;color:#bff6ff}}
.footer{{font-size:{10 if is_mobile else 12}px;color:#89a2b4;display:flex;justify-content:space-between;gap:10px}}
</style></head><body><main class="shell"><div class="top"><div class="brand">NEXT ATHLETE OS</div><div class="pill">RC38 · {name}</div></div><section class="grid"><div class="stage"><div class="floor"></div><img class="sprite s1" src="{img1}"><img class="sprite s2" src="{img2}"><img class="sprite s3" src="{img3}"><span class="dot d1"></span><span class="dot d2"></span><span class="dot d3"></span></div><aside class="panel"><h1>Train-along motion QA</h1><p>Sprite-linked runtime landmarks styr fötter/händer. Rensade atlas-fragment är borttagna innan skelett/ankare används.</p><div class="check"><b>✓</b><span>Fotankare väljs från bound landmarks, inte “första bästa” pixel/fot.</span></div><div class="check"><b>✓</b><span>Skelett och PNG speglas tillsammans via samma runtime flip.</span></div><div class="check"><b>✓</b><span>MediaPipe kan kopplas ovanpå samma schema senare.</span></div><div class="badges"><span class="badge">athlete-b</span><span class="badge">footwork</span><span class="badge">medball cleaned</span><span class="badge">offline QA</span></div></aside></section><div class="footer"><span>Static fallback render via WeasyPrint</span><span>no Chromium screenshot available</span></div></main></body></html>'''

for name,(w,h) in VIEWS.items():
    html=ROOT/f'qa_rc38_{name}.html'
    pdf=QA/f'rc38_{name}_static_qa.pdf'
    png_prefix=QA/f'rc38_{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(png_prefix)],check=True)
    print(QA/f'rc38_{name}_static_qa.png')
