#!/usr/bin/env python3
"""Verify the ZOPAIR WORKS ownership register.

    python3 verify.py              check every certificate
    python3 verify.py ZOP-2026-001 check one

A fingerprint is sha256 of  "<no>|<slug>|<name_en>|<url>|<issued>"
with name_en as plain text (HTML entities resolved).
"""
import hashlib, html, json, os, sys

here = os.path.dirname(os.path.abspath(__file__))
reg = json.load(open(os.path.join(here, "registry.json"), encoding="utf-8"))
want = sys.argv[1] if len(sys.argv) > 1 else None
bad = checked = 0
for c in reg["certificates"]:
    if want and c["no"] != want:
        continue
    raw = "|".join([c["no"], c["slug"], html.unescape(c["name_en"]), c["url"], reg["issued"]])
    got = hashlib.sha256(raw.encode()).hexdigest()
    ok = got == c["fingerprint"]
    checked += 1
    bad += 0 if ok else 1
    print(("OK   " if ok else "FAIL ") + c["no"] + "  " + html.unescape(c["name_en"]))
if not want:
    print("\n%d certificates checked, %d failed." % (checked, bad))
sys.exit(1 if bad else 0)
