"""Repo hygiene: every registered policy provider has a docs page in the nav.

A provider registered in ``strands_robots/registry/policies.json`` is part of
the public API: ``create_policy("<provider>")`` works for it. If the MkDocs
site has no page for that provider, a user who discovers it via
``list_providers()`` lands on a dead end. This guard ties the registry to the
documentation so a new provider cannot ship without a docs page wired into the
``mkdocs.yml`` navigation.

``mock`` is exempt: it is a built-in testing stub documented inline in the
policy overview, not a standalone provider page.
"""

from __future__ import annotations

import json
import re
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent
POLICIES_JSON = REPO_ROOT / "strands_robots" / "registry" / "policies.json"
MKDOCS_YML = REPO_ROOT / "mkdocs.yml"
DOCS_DIR = REPO_ROOT / "docs"

# Providers documented inline rather than on a standalone page.
_INLINE_DOCUMENTED = {"mock"}


def _registered_providers() -> set[str]:
    data = json.loads(POLICIES_JSON.read_text(encoding="utf-8"))
    return set(data["providers"].keys())


def _nav_policy_pages() -> set[str]:
    """Return the policy doc stems referenced in the mkdocs nav."""
    nav = MKDOCS_YML.read_text(encoding="utf-8")
    # Normalise hyphens to underscores so a provider id (``lerobot_local``)
    # matches a hyphenated doc stem (``lerobot-local.md``).
    return {stem.replace("-", "_") for stem in re.findall(r"policies/([a-z0-9_-]+)\.md", nav)}


def test_every_provider_has_a_docs_page_in_nav() -> None:
    """Each non-mock registered provider has a docs page wired into the nav."""
    providers = _registered_providers() - _INLINE_DOCUMENTED
    nav_pages = _nav_policy_pages()
    missing = sorted(p for p in providers if p not in nav_pages)
    assert not missing, (
        f"registered policy providers with no docs page in mkdocs.yml nav: "
        f"{missing}. Add docs/policies/<provider>.md and a nav entry under "
        f"'Policy Providers'."
    )


def test_nav_policy_pages_exist_on_disk() -> None:
    """Every policy page referenced in the nav resolves to a real file."""
    nav = MKDOCS_YML.read_text(encoding="utf-8")
    stems = re.findall(r"policies/([a-z0-9_-]+)\.md", nav)
    missing = sorted(stem for stem in stems if not (DOCS_DIR / "policies" / f"{stem}.md").is_file())
    assert not missing, f"mkdocs.yml nav references missing policy docs: {missing}"
