#!/usr/bin/env python3
"""
NR Draw · winner card generator
===============================
Renders the WhatsApp winner card with the real winning code.
Run after the armed draw, alongside record_draw.py, so the card,
the film, and the page all carry the same result.

Usage:
  python3 make_winner_card.py --code COWYN09 --entries entries.txt
  python3 make_winner_card.py --code COWYN09 --entries entries.txt --date 22.09.2026

Output: NR_Winner_Card_<CODE>.png · 1080x1350 · ready to send.
The card is private material for the winner's WhatsApp thread only;
the public record stays code-only on the draw page.
"""
import argparse, pathlib, sys

TEMPLATE = pathlib.Path(__file__).parent / "NR_Winner_Card_Template.html"

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--code", required=True)
    ap.add_argument("--entries", required=True, help="entries.txt, one code per entry line")
    ap.add_argument("--date", default="22.09.2026")
    ap.add_argument("--out", default=None)
    a = ap.parse_args()

    entries = [ln.strip().upper() for ln in open(a.entries) if ln.strip()]
    code = a.code.upper()
    if code not in entries:
        sys.exit(f"Refusing to render: {code} is not in the entries register.")

    html = TEMPLATE.read_text()
    html = html.replace("{{CODE}}", code)
    html = html.replace("{{META}}", f"DRAWN {a.date} · {len(entries)} ENTRIES")
    work = TEMPLATE.parent / f"_card_{code}.html"
    work.write_text(html)

    out = pathlib.Path(a.out or f"NR_Winner_Card_{code}.png")
    from playwright.sync_api import sync_playwright
    with sync_playwright() as p:
        b = p.chromium.launch()
        pg = b.new_page(viewport={"width": 1080, "height": 1350})
        pg.goto(work.resolve().as_uri())
        pg.wait_for_timeout(900)
        pg.screenshot(path=str(out))
        b.close()
    work.unlink()
    print(f"→ {out}")

if __name__ == "__main__":
    main()
