#!/usr/bin/env python3
"""Guard CARBON HTML surfaces against regressions on iPhone-sized viewports."""

from __future__ import annotations

import argparse
from pathlib import Path


def require(text: str, needle: str, label: str, failures: list[str]) -> None:
    if needle not in text:
        failures.append(f"{label}: missing {needle!r}")


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--root", default=Path(__file__).resolve().parents[1])
    parser.add_argument("--site-root")
    args = parser.parse_args()
    root = Path(args.root).resolve()
    failures: list[str] = []

    static_pages = [
        root / "ui/carbon-command-report-app.html",
        root / "ui/command-progress.html",
        root / "ui/execution-plan.html",
        root / "ui/human.html",
        root / "ui/feedback.html",
        root / "ui/report-loader.html",
        root / "ui/settings.html",
        root / "ui/tests.html",
    ]
    generators = [
        root / "runtime/carbon/scripts/carbon_bake.py",
        root / "runtime/carbon/scripts/carbon_chatbot.py",
        root / "runtime/carbon/scripts/carbon_command_report.py",
        root / "runtime/carbon/scripts/carbon_framework.py",
        root / "runtime/carbon/scripts/carbon_intelligence.py",
        root / "runtime/carbon/scripts/carbon_learn.py",
        root / "runtime/carbon/scripts/carbon_persona_journeys.py",
        root / "runtime/carbon/scripts/carbon_run.py",
    ]
    for path in static_pages + generators:
        require(path.read_text(encoding="utf-8"), "viewport-fit=cover", str(path.relative_to(root)), failures)

    theme = (root / "runtime/carbon/scripts/carbon_run.py").read_text(encoding="utf-8")
    for needle in ("100svh", "safe-area-inset-bottom", "min-height: 44px", "font-size: 16px"):
        require(theme, needle, "shared report theme", failures)

    visuals = (root / "runtime/carbon/scripts/carbon_report_visuals.py").read_text(encoding="utf-8")
    for needle in ("@media(max-width:480px)", ".matrix-point span{display:none}", ".rank-track{grid-column:1/-1"):
        require(visuals, needle, "report visuals", failures)

    intelligence = (root / "runtime/carbon/scripts/carbon_intelligence.py").read_text(encoding="utf-8")
    require(intelligence, "@media(max-width:420px)", "intelligence report", failures)
    require(intelligence, ".toolbar{{position:static;grid-template-columns:1fr}}", "intelligence report", failures)

    chatbot = (root / "runtime/carbon/scripts/carbon_chatbot.py").read_text(encoding="utf-8")
    require(chatbot, "@media(max-width:560px)", "chatbot report", failures)
    require(chatbot, ".toolbar{position:static;grid-template-columns:1fr}", "chatbot report", failures)

    if args.site_root:
        site = Path(args.site_root).resolve()
        for name in ("index.html", "carbon-demo.html", "brand.html", "docs.html", "install-carbon.html", "carbon-beta.html"):
            require((site / name).read_text(encoding="utf-8"), "viewport-fit=cover", f"site/{name}", failures)
        site_css = (site / "carbon-site.css").read_text(encoding="utf-8")
        for needle in ("100svh", "safe-area-inset-bottom", "font-size: 16px", ".actions { display: grid"):
            require(site_css, needle, "site/carbon-site.css", failures)

    if failures:
        print("FAIL: mobile HTML contract")
        for failure in failures:
            print(f"- {failure}")
        return 1
    print(f"PASS: {len(static_pages)} static pages and {len(generators)} report generators retain the iPhone mobile contract")
    return 0


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