#!/usr/bin/env python3
"""Meta 캐러셀 25장 일괄 렌더링 — Playwright"""

from pathlib import Path
from playwright.sync_api import sync_playwright

BASE = Path("/home/jay/workspace/output/meta-ads")
ANGLES = ["angle-A", "angle-B", "angle-C", "angle-D", "angle-E"]
SLIDES = [f"slide-{i:02d}" for i in range(1, 6)]


def render_all():
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(viewport={"width": 1080, "height": 1080})

        total = 0
        errors = []

        for angle in ANGLES:
            for slide in SLIDES:
                html_path = BASE / angle / "production" / f"{slide}.html"
                png_path = BASE / angle / "production" / f"{slide}.png"

                if not html_path.exists():
                    errors.append(f"MISSING: {html_path}")
                    continue

                try:
                    page.goto(f"file://{html_path.resolve()}", wait_until="networkidle")
                    page.wait_for_timeout(2000)  # Font loading
                    page.screenshot(path=str(png_path), type="png", full_page=False)
                    size_kb = png_path.stat().st_size / 1024
                    print(f"OK: {angle}/{slide}.png ({size_kb:.0f} KB)")
                    total += 1
                except Exception as e:
                    errors.append(f"ERROR: {angle}/{slide} — {e}")
                    print(f"FAIL: {angle}/{slide} — {e}")

        browser.close()

    print(f"\n=== 렌더링 완료: {total}/25 성공 ===")
    if errors:
        print("오류 목록:")
        for e in errors:
            print(f"  - {e}")


if __name__ == "__main__":
    render_all()
