#!/usr/bin/env python3
"""Render all 9-cell banner HTML files to PNG using Playwright."""
import os
import sys
from pathlib import Path
from playwright.sync_api import sync_playwright

BANNER_ROOT = Path("/home/jay/workspace/output/banners")

CELLS = [
    ("cell-1-incar-fair", [
        ("meta-feed-1080x1080", 1080, 1080),
        ("google-resp-1200x628", 1200, 628),
    ]),
    ("cell-2-incar-leader", [
        ("meta-feed-1080x1080", 1080, 1080),
        ("google-resp-1200x628", 1200, 628),
    ]),
    ("cell-3-incar-support", [
        ("meta-feed-1080x1080", 1080, 1080),
        ("google-resp-1200x628", 1200, 628),
    ]),
    ("cell-4-ga-fair", [
        ("meta-feed-1080x1080", 1080, 1080),
        ("google-resp-1200x628", 1200, 628),
    ]),
    ("cell-5-ga-leader", [
        ("meta-feed-1080x1080", 1080, 1080),
        ("google-resp-1200x628", 1200, 628),
    ]),
    ("cell-6-ga-support", [
        ("meta-feed-1080x1080", 1080, 1080),
        ("google-resp-1200x628", 1200, 628),
    ]),
    ("cell-7-snu-fair", [
        ("meta-feed-1080x1080", 1080, 1080),
        ("google-resp-1200x628", 1200, 628),
    ]),
    ("cell-8-snu-leader", [
        ("meta-feed-1080x1080", 1080, 1080),
        ("google-resp-1200x628", 1200, 628),
    ]),
    ("cell-9-snu-support", [
        ("meta-feed-1080x1080", 1080, 1080),
        ("google-resp-1200x628", 1200, 628),
    ]),
]


def render_all():
    success = 0
    fail = 0
    results = []

    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)

        for cell_dir, variants in CELLS:
            for name, width, height in variants:
                html_path = BANNER_ROOT / cell_dir / f"{name}.html"
                png_path = BANNER_ROOT / cell_dir / f"{name}.png"

                if not html_path.exists():
                    print(f"[SKIP] {html_path} not found")
                    fail += 1
                    results.append(f"FAIL: {html_path} missing")
                    continue

                try:
                    page = browser.new_page(viewport={"width": width, "height": height})
                    page.goto(f"file://{html_path}", wait_until="networkidle", timeout=30000)
                    page.wait_for_timeout(1000)  # Wait for fonts to load
                    page.screenshot(path=str(png_path), type="png")
                    page.close()

                    if png_path.exists() and png_path.stat().st_size > 0:
                        print(f"[OK] {png_path} ({png_path.stat().st_size:,} bytes)")
                        success += 1
                        results.append(f"OK: {png_path}")
                    else:
                        print(f"[FAIL] {png_path} empty or missing")
                        fail += 1
                        results.append(f"FAIL: {png_path} empty")
                except Exception as e:
                    print(f"[ERROR] {html_path}: {e}")
                    fail += 1
                    results.append(f"ERROR: {html_path}: {e}")

        browser.close()

    print(f"\n=== Summary ===")
    print(f"Success: {success}/18")
    print(f"Fail: {fail}/18")

    return success, fail, results


if __name__ == "__main__":
    s, f, r = render_all()
    sys.exit(0 if f == 0 else 1)
