#!/usr/bin/env python3
"""
render.py — Render every slide-*.html in this folder to PNG at 1440x1800.

Usage:
    python3 render.py            # renders all slide-*.html next to this script
    python3 render.py slide-01-hook.html

Setup (run once):
    pip install playwright --break-system-packages
    python3 -m playwright install chromium
"""

import glob
import os
import sys
from playwright.sync_api import sync_playwright

W, H = 1440, 1800


def render(html_files):
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(
            viewport={"width": W, "height": H},
            device_scale_factor=1,
        )
        for html in html_files:
            abspath = os.path.abspath(html)
            out = os.path.splitext(abspath)[0] + ".png"
            page.goto(f"file://{abspath}")
            page.wait_for_timeout(3500)  # Google Fonts need ~3.5s, never skip
            page.screenshot(path=out, clip={"x": 0, "y": 0, "width": W, "height": H})
            print(f"rendered {os.path.basename(out)}")
        browser.close()


def main():
    if len(sys.argv) > 1:
        files = sys.argv[1:]
    else:
        here = os.path.dirname(os.path.abspath(__file__))
        files = sorted(glob.glob(os.path.join(here, "slide-*.html")))
    if not files:
        print("No slide-*.html files found.")
        sys.exit(1)
    render(files)


if __name__ == "__main__":
    main()
