"""Route tests against the real federation (the spec's named acceptance cases)."""

from __future__ import annotations

import pytest
from fastapi.testclient import TestClient

from mgf.fed_web.discovery import DiskProvider, discover_projects
from mgf.fed_web.lenses import assemble_meta, build_lens

from .conftest import REAL_PROJECTS


def _provider(slug: str) -> DiskProvider:
    return next(p.provider for p in discover_projects() if p.slug == slug)


@pytest.mark.smoke
def test_app_boots(client: TestClient) -> None:
    assert client.get("/").status_code == 200


def test_overview_shows_both_projects_and_their_sync_states(client: TestClient) -> None:
    body = client.get("/").text
    metas = {p.slug: assemble_meta(p.slug, p.provider) for p in discover_projects()}
    for slug in REAL_PROJECTS:
        assert slug in metas, f"{slug} should be discovered"
        meta = metas[slug]
        assert meta.name in body
        chip = f"status-chip--{meta.sync_state.lower().replace('_', '-')}"
        assert chip in body, f"{slug}'s {meta.sync_state} chip should render"


def test_standard_architecture_renders_the_graph(client: TestClient) -> None:
    resp = client.get("/p/mgf-standard/architecture")
    assert resp.status_code == 200
    view = build_lens("architecture", _provider("mgf-standard"))
    assert view["ok"] and view["mode"] == "graph"
    node_count = view["counts"]["nodes"]
    assert node_count >= 30  # the federation graph (35 at time of writing)
    assert str(node_count) in resp.text
    assert "mgf-common" in resp.text  # a representative node label


def test_fedres_roadmap_renders(client: TestClient) -> None:
    resp = client.get("/p/mgf-fedres/roadmap")
    assert resp.status_code == 200
    body = resp.text
    assert "Roadmap to" in body
    view = build_lens("roadmap", _provider("mgf-fedres"))
    assert view["ok"] and view["stages"]
    assert view["stages"][0]["label"] in body
    for column in ("Risk", "Effort", "ETA", "Deps", "Owner"):
        assert column in body


def test_default_lens_is_roadmap(client: TestClient) -> None:
    assert "Roadmap to" in client.get("/p/mgf-fedres").text


def test_bogus_project_is_honest_unknown_not_500(client: TestClient) -> None:
    resp = client.get("/p/nope")
    assert resp.status_code == 200  # honest-absence, never a crash
    body = resp.text
    assert "UNKNOWN" in body
    assert "wired" in body


def test_absent_lens_shows_nothing_reported(client: TestClient) -> None:
    # mgf-standard reports no roadmap.json -> explicit "Nothing reported yet".
    resp = client.get("/p/mgf-standard/roadmap")
    assert resp.status_code == 200
    assert "Nothing reported yet" in resp.text


def test_unknown_lens_falls_back_to_default(client: TestClient) -> None:
    resp = client.get("/p/mgf-fedres/does-not-exist")
    assert resp.status_code == 200
    assert "Roadmap to" in resp.text  # clamped to the default lens, no crash


def test_print_button_present(client: TestClient) -> None:
    # Everything must be printable (spec §5.8); the readout carries a print action.
    assert "window.print()" in client.get("/").text
