"""The brand-web kit ships the expected assets and a usable accessor."""

from __future__ import annotations

import pytest

from mgf.brand_web import __version__, static_dir, templates_dir


def test_version_present() -> None:
    assert __version__


@pytest.mark.smoke
def test_static_dir_exists() -> None:
    assert static_dir().is_dir()


@pytest.mark.smoke
def test_templates_dir_ships_the_mgf_pack() -> None:
    """0.4.0: the Jinja template pack is package data under the mgf/ namespace."""
    for name in ("console.html", "macros.html", "styleguide.html"):
        assert (templates_dir() / "mgf" / name).is_file(), name


@pytest.mark.smoke
@pytest.mark.parametrize(
    "name",
    [
        "tokens.css",
        "global-tokens.css",
        "base.css",
        "components.css",
        "instruments.css",
        "console.css",
        "fonts.css",
        "README.md",
    ],
)
def test_core_files_present(name: str) -> None:
    assert (static_dir() / name).is_file()


def test_six_woff2_fonts_ship() -> None:
    fonts = sorted((static_dir() / "fonts").glob("*.woff2"))
    assert len(fonts) == 6, [f.name for f in fonts]


def test_logo_and_pattern_assets_ship() -> None:
    assert (static_dir() / "logo" / "magogi-favicon.svg").is_file()
    assert list((static_dir() / "pattern").glob("*.svg"))


def test_tokens_entry_imports_the_generated_layer() -> None:
    """0.3.0: tokens.css is the linked entry — it pulls in the canon layer."""
    txt = (static_dir() / "tokens.css").read_text(encoding="utf-8")
    assert '@import url("global-tokens.css");' in txt


def test_generated_layer_is_the_canon_source() -> None:
    """global-tokens.css is a verbatim copy of the GENERATED mgf-brand file:
    the provenance header and the canon palette must be present, exactly as
    gen_tokens.py emits them (never hand-edited here)."""
    txt = (static_dir() / "global-tokens.css").read_text(encoding="utf-8")
    assert "GENERATED by gen_tokens.py" in txt
    for token in ("--mgf-indigo", "--mgf-gold", "--mgf-teal", "--mgf-color-focus-ring"):
        assert token in txt, token


def test_tokens_define_instrument_states() -> None:
    """busy/derived are canon since Decision 24 (founder-enacted 2026-07-04):
    raw hues + semantic state set in the generated layer; the legacy --state-*
    spellings survive as deprecated aliases in tokens.css."""
    canon = (static_dir() / "global-tokens.css").read_text(encoding="utf-8")
    assert "--mgf-busy" in canon
    assert "--mgf-derived" in canon
    assert "--mgf-color-state-busy" in canon
    assert "--mgf-color-state-derived" in canon
    aliases = (static_dir() / "tokens.css").read_text(encoding="utf-8")
    assert "--state-busy" in aliases
    assert "--state-derived" in aliases


def test_every_consumed_variable_resolves() -> None:
    """No undefined-token lookups: every var(--x) referenced by any kit sheet
    is declared somewhere in the kit's CSS (tokens/global-tokens/base/
    components/instruments — including component-local custom properties).
    This is the guard that killed the pre-0.3 gappy-scale bug class
    (e.g. --space-5 consumed but never defined)."""
    import re

    sheets = [
        "tokens.css",
        "global-tokens.css",
        "base.css",
        "components.css",
        "instruments.css",
        "console.css",
        "fonts.css",
    ]
    defined: set[str] = set()
    referenced: dict[str, set[str]] = {}
    for name in sheets:
        css = (static_dir() / name).read_text(encoding="utf-8")
        defined |= set(re.findall(r"(--[a-zA-Z0-9-]+)\s*:", css))
        referenced[name] = set(re.findall(r"var\((--[a-zA-Z0-9-]+)", css))
    missing = {
        name: sorted(v for v in refs if v not in defined) for name, refs in referenced.items()
    }
    missing = {name: vals for name, vals in missing.items() if vals}
    assert not missing, f"undefined token lookups: {missing}"


def test_instruments_layer_ships_component_families() -> None:
    """The operator-instrument layer defines its headline component classes."""
    txt = (static_dir() / "instruments.css").read_text(encoding="utf-8")
    for cls in (
        ".status-chip",
        ".status-indicator",
        ".progress",
        ".console",
        ".live-pane",
        # 0.4.0 — the families lifted from the fed-web/test-web app layers:
        ".tstate",
        ".origin-badge",
        ".derived-tag",
        ".absence",
    ):
        assert cls in txt, cls


def test_console_shell_ships_the_mgf_console_family() -> None:
    """0.4.0: console.css carries the shared operator shell + the skip link,
    themed by canonical --mgf-* tokens (never legacy aliases — new-code rule)."""
    txt = (static_dir() / "console.css").read_text(encoding="utf-8")
    for cls in (
        ".mgf-console",
        ".mgf-console__topbar",
        ".mgf-console__mark",
        ".mgf-console__pill",
        ".mgf-console__rail",
        ".mgf-console__nav",
        ".mgf-console__legend",
        ".mgf-console__main",
        ".skip-link",
        "@media print",
    ):
        assert cls in txt, cls
    import re

    legacy = {v for v in re.findall(r"var\((--[a-zA-Z0-9-]+)", txt) if not v.startswith("--mgf-")}
    assert not legacy, f"console.css must consume only --mgf-* tokens: {sorted(legacy)}"
