#!/usr/bin/env python3
"""Batch C 프로덕션 배너 렌더링 — Playwright"""

from pathlib import Path
from playwright.sync_api import sync_playwright

BASE = Path("/home/jay/workspace/output/meta-ads/production/batch-c")

BANNERS = [
    ("c41", "c41-1200x628.html", 1200, 628),
    ("c41", "c41-1200x1200.html", 1200, 1200),
    ("c44", "c44-1200x628.html", 1200, 628),
    ("c44", "c44-1200x1200.html", 1200, 1200),
    ("c45", "c45-1200x628.html", 1200, 628),
    ("c45", "c45-1200x1200.html", 1200, 1200),
    ("c48", "c48-1200x628.html", 1200, 628),
    ("c48", "c48-1200x1200.html", 1200, 1200),
    ("c49", "c49-1200x628.html", 1200, 628),
    ("c49", "c49-1200x1200.html", 1200, 1200),
]


def render_all():
    with sync_playwright() as p:
        browser = p.chromium.launch()
        total = 0
        errors = []

        for concept, html_file, width, height in BANNERS:
            html_path = BASE / concept / html_file
            png_path = html_path.with_suffix(".png")

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

            try:
                page = browser.new_page(viewport={"width": width, "height": height})
                page.goto(f"file://{html_path.resolve()}", wait_until="networkidle")
                page.wait_for_timeout(3000)  # Font loading
                page.screenshot(path=str(png_path), type="png", full_page=False)
                page.close()
                size_kb = png_path.stat().st_size / 1024
                print(f"OK: {concept}/{html_file} → {png_path.name} ({size_kb:.0f} KB)")
                total += 1
            except Exception as e:
                errors.append(f"ERROR: {concept}/{html_file} — {e}")
                print(f"FAIL: {concept}/{html_file} — {e}")

        browser.close()

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


if __name__ == "__main__":
    render_all()
