#!/usr/bin/env python3
"""Static smoke-test for the trimmed management home.

The old browser sweep generated a very large artifact and frequently stalled.
For the cleanup goal, the important contract is that the home stays a compact
management index and links only active surfaces.
"""
from __future__ import annotations

import json
import re
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
WEB = ROOT / "web"
OUT = ROOT / "out"


def main() -> int:
    html = (WEB / "index.html").read_text(encoding="utf-8")
    links = re.findall(r'href="([^"]+)"', html)
    required = {
        "game.html?game=1",
        "map_review.html",
        "resource_reference_review.html",
        "menu_review.html",
        "menu_descriptor_stack_review.html",
        "cns_rect_review.html",
        "battle_formula_calculator.html",
        "scene_event_runtime_evidence_handoff.html",
    }
    missing = sorted(required - set(links))
    if missing:
        raise RuntimeError(f"home missing required links: {missing}")
    forbidden = [link for link in links if "_audit.html" in link]
    if forbidden:
        raise RuntimeError(f"home exposes legacy audit links: {forbidden}")

    payload = {
        "kind": "home-landing-static-smoke",
        "titlePresent": "환세취호전 복원 관리 홈" in html,
        "rowCount": html.count("data-landing-row"),
        "linkCount": len(links),
        "requiredLinks": sorted(required),
        "legacyAuditLinks": forbidden,
        "resourceMenuLinksPromoted": all(
            rel in links
            for rel in (
                "resource_reference_review.html",
                "menu_review.html",
                "menu_descriptor_stack_review.html",
            )
        ),
    }
    OUT.mkdir(exist_ok=True)
    (OUT / "home_landing_browser_smoke.json").write_text(
        json.dumps(payload, ensure_ascii=False, indent=2) + "\n",
        encoding="utf-8",
    )
    print("verify_home_landing_browser ok")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
