"""Small read-only release smoke check for public project response headers.

Run: python3 tools/check_public_headers.py
This verifies public responses, not privacy, authentication or exploit resistance.
"""
from concurrent.futures import ThreadPoolExecutor
import urllib.request

SITES = (
    ("Portfolio", "https://jay-heffley-portfolio.onrender.com/", "DENY", "none"),
    ("Estimator / Cloudflare", "https://gc-field-estimator.pages.dev/", "DENY", "none"),
    ("Estimator / Render", "https://gc-field-estimator.onrender.com/", "DENY", "none"),
    ("Hollowmouth House", "https://hollowmouth-chronicles.onrender.com/", "SAMEORIGIN", "self"),
    ("Code Auditor", "https://clean-code-t6w2.onrender.com/", "SAMEORIGIN", "self"),
)

def check(site):
    name, url, frame, ancestor = site
    req = urllib.request.Request(url, method="GET", headers={"User-Agent":"JayPortfolioHeaderCheck/1.0"})
    try:
        with urllib.request.urlopen(req, timeout=15) as r:
            h = r.headers
            rules = {"HTTPS": r.status == 200 and r.url == url,
                     "frame header": h.get("X-Frame-Options", "").upper() == frame,
                     "frame policy": f"frame-ancestors '{ancestor}'" in ";".join(h.get_all("Content-Security-Policy", [])),
                     "HSTS": bool(h.get("Strict-Transport-Security")),
                     "nosniff": h.get("X-Content-Type-Options", "").lower() == "nosniff",
                     "referrer policy": bool(h.get("Referrer-Policy")),
                     "permissions policy": bool(h.get("Permissions-Policy"))}
            failed = [k for k, ok in rules.items() if not ok]
    except (OSError, ValueError) as exc:
        failed = [type(exc).__name__]
    return name, failed

if __name__ == "__main__":
    with ThreadPoolExecutor(max_workers=5) as pool:
        results = list(pool.map(check, SITES))
    for name, failed in results:
        print(f"{name}: {'check ' + ', '.join(failed) if failed else 'PASS'}")
    raise SystemExit(any(failed for _, failed in results))
