"""Implemented REST path registry for UI catalog and surface probes.

CORE_* entries are deep lab handlers. Broadcom Automation API routes from
``universe.json`` (generated from the public operations index) fill the rest as
thin stubs — each ``(verb, path)`` is registered separately in ``stub_surface``.
"""

from __future__ import annotations

import json
from functools import lru_cache
from pathlib import Path

ACTIVE_STATUSES = frozenset({"implemented", "stub"})

# verb + path template → status (deep handlers)
CORE_IMPLEMENTED: dict[tuple[str, str], str] = {
    ("POST", "/api/session"): "implemented",
    ("DELETE", "/api/session"): "implemented",
    ("GET", "/api/session"): "implemented",
    ("POST", "/rest/com/vmware/cis/session"): "implemented",
    ("GET", "/rest/com/vmware/cis/session"): "implemented",
    ("DELETE", "/rest/com/vmware/cis/session"): "implemented",
    ("GET", "/api/cis/tasks"): "implemented",
    ("POST", "/api/cis/tasks"): "implemented",
    ("GET", "/api/cis/tasks/{task}"): "implemented",
    ("GET", "/api/appliance/system/version"): "implemented",
    ("GET", "/api/appliance/health/system"): "implemented",
    ("GET", "/api/appliance/networking"): "implemented",
    ("GET", "/api/appliance/networking/dns/hostname"): "implemented",
    ("PUT", "/api/appliance/networking/dns/hostname"): "implemented",
    ("POST", "/api/appliance/networking/dns/hostname"): "implemented",
    ("GET", "/api/appliance/networking/dns/servers"): "implemented",
    ("PUT", "/api/appliance/networking/dns/servers"): "implemented",
    ("POST", "/api/appliance/networking/dns/servers"): "implemented",
    ("GET", "/api/appliance/networking/dns/domains"): "implemented",
    ("PUT", "/api/appliance/networking/dns/domains"): "implemented",
    ("POST", "/api/appliance/networking/dns/domains"): "implemented",
    ("GET", "/api/appliance/timesync"): "implemented",
    ("GET", "/api/vcenter/vm"): "implemented",
    ("POST", "/api/vcenter/vm"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}"): "implemented",
    ("DELETE", "/api/vcenter/vm/{vm}"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/power"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/power"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/guest/identity"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/guest/local-filesystem"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/guest/filesystem"): "implemented",
    ("PUT", "/api/vcenter/vm/{vm}/guest/filesystem"): "implemented",
    ("DELETE", "/api/vcenter/vm/{vm}/guest/filesystem"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/guest/filesystem/files"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/tools"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/hardware"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/hardware/cpu"): "implemented",
    ("PATCH", "/api/vcenter/vm/{vm}/hardware/cpu"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/hardware/memory"): "implemented",
    ("PATCH", "/api/vcenter/vm/{vm}/hardware/memory"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/hardware/disk"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/hardware/disk"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/hardware/ethernet"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/hardware/ethernet"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/hardware/boot"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/snapshots"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/snapshots"): "implemented",
    ("DELETE", "/api/vcenter/vm/{vm}/snapshots/{snapshot}"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/snapshots/{snapshot}"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/clone"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/relocate"): "implemented",
    ("GET", "/api/vcenter/host"): "implemented",
    ("GET", "/api/vcenter/host/{host}"): "implemented",
    ("POST", "/api/vcenter/host/{host}/maintenance"): "implemented",
    ("GET", "/api/vcenter/datastore"): "implemented",
    ("GET", "/api/vcenter/datastore/{datastore}"): "implemented",
    ("GET", "/api/vcenter/datastore/{datastore}/files"): "implemented",
    ("POST", "/api/vcenter/datastore/{datastore}/files"): "implemented",
    ("GET", "/api/vcenter/network"): "implemented",
    ("GET", "/api/vcenter/datacenter"): "implemented",
    ("POST", "/api/vcenter/datacenter"): "implemented",
    ("DELETE", "/api/vcenter/datacenter/{datacenter}"): "implemented",
    ("GET", "/api/vcenter/cluster"): "implemented",
    ("POST", "/api/vcenter/cluster"): "implemented",
    ("DELETE", "/api/vcenter/cluster/{cluster}"): "implemented",
    ("GET", "/api/vcenter/folder"): "implemented",
    ("POST", "/api/vcenter/folder"): "implemented",
    ("POST", "/api/vcenter/folder/{folder}"): "implemented",
    ("DELETE", "/api/vcenter/folder/{folder}"): "implemented",
    ("GET", "/api/vcenter/resource-pool"): "implemented",
    ("POST", "/api/vcenter/resource-pool"): "implemented",
    ("DELETE", "/api/vcenter/resource-pool/{resource_pool}"): "implemented",
    ("GET", "/api/vcenter/network/dvs"): "implemented",
    ("POST", "/api/vcenter/network/dvs"): "implemented",
    ("POST", "/api/vcenter/network/dvpg"): "implemented",
    ("GET", "/api/cis/tagging/category"): "implemented",
    ("POST", "/api/cis/tagging/category"): "implemented",
    ("GET", "/api/cis/tagging/category/{category_id}"): "implemented",
    ("DELETE", "/api/cis/tagging/category/{category_id}"): "implemented",
    ("GET", "/api/cis/tagging/tag"): "implemented",
    ("POST", "/api/cis/tagging/tag"): "implemented",
    ("GET", "/api/cis/tagging/tag/{tag_id}"): "implemented",
    ("DELETE", "/api/cis/tagging/tag/{tag_id}"): "implemented",
    ("POST", "/api/cis/tagging/tag-association"): "implemented",
    ("GET", "/api/content/library"): "implemented",
    ("GET", "/api/content/library/{library_id}"): "implemented",
    ("GET", "/api/content/local-library"): "implemented",
    ("POST", "/api/content/local-library"): "implemented",
    ("GET", "/api/content/local-library/{library_id}"): "implemented",
    ("DELETE", "/api/content/local-library/{library_id}"): "implemented",
    ("GET", "/api/content/library/item"): "implemented",
    ("POST", "/api/content/library/item"): "implemented",
    ("GET", "/api/content/library/item/{library_item_id}"): "implemented",
    ("DELETE", "/api/content/library/item/{library_item_id}"): "implemented",
    ("POST", "/api/content/library/item/update-session"): "implemented",
    ("GET", "/api/content/library/item/update-session/{session_id}"): "implemented",
    ("POST", "/api/content/library/item/update-session/{session_id}"): "implemented",
    ("POST", "/api/content/library/item/update-session/{session_id}/file"): "implemented",
    ("POST", "/api/content/library/item/download-session"): "implemented",
    ("GET", "/api/content/library/item/download-session/{session_id}"): "implemented",
    ("GET", "/api/content/library/item/download-session/{session_id}/file"): "implemented",
    ("POST", "/api/vcenter/ovf/library-item/{item_id}"): "implemented",
    ("GET", "/api/vcenter/storage/policies"): "implemented",
    ("GET", "/api/vcenter/storage/policies/{policy}/vm"): "implemented",
    ("GET", "/api/vcenter/privilege"): "implemented",
    ("GET", "/api/vcenter/authorization/roles"): "implemented",
    ("GET", "/api/vcenter/authorization/permissions"): "implemented",
    ("POST", "/api/vcenter/authorization/permissions"): "implemented",
    ("DELETE", "/api/vcenter/authorization/permissions/{permission_id}"): "implemented",
    ("GET", "/api/vcenter/namespaces"): "implemented",
    ("GET", "/api/vcenter/namespace-management/nsx-tier0-gateway"): "implemented",
    ("GET", "/api/vcenter/namespace-management/networks"): "implemented",
    ("GET", "/api/vcenter/namespace-management/virtual-machine-classes"): "implemented",
    ("GET", "/api/vcenter/identity/providers"): "implemented",
    ("POST", "/api/vcenter/identity/providers"): "implemented",
    ("GET", "/api/vcenter/identity/providers/{provider}"): "implemented",
    ("PUT", "/api/vcenter/identity/providers/{provider}"): "implemented",
    ("PATCH", "/api/vcenter/identity/providers/{provider}"): "implemented",
    ("DELETE", "/api/vcenter/identity/providers/{provider}"): "implemented",
    ("GET", "/api/vcenter/certificate-management/vcenter/tls-csr"): "implemented",
    ("POST", "/api/vcenter/certificate-management/vcenter/tls-csr"): "implemented",
    ("GET", "/api/vcenter/certificate-management/vcenter/trusted-root-chains"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/guest/customization"): "implemented",
    ("GET", "/api/vcenter/certificate-management/vcenter/tls"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/guest/networking"): "implemented",
    ("GET", "/api/vcenter/vm/{vm}/guest/power"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/guest/power"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/tools"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/console/tickets"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}/guest/customization"): "implemented",
    ("POST", "/api/vcenter/vm/{vm}"): "implemented",
    ("GET", "/api/vcenter/host/{host}/storage/storage-device"): "implemented",
    ("GET", "/api/vcenter/host/{host}/networking"): "implemented",
    ("GET", "/api/vcenter/folder/{folder}/children"): "implemented",
    ("GET", "/api/vapi/metadata/metamodel/service"): "implemented",
    ("GET", "/api/vapi/metadata/authentication/component"): "implemented",
    ("GET", "/api/vcenter/activity-history"): "implemented",
    ("GET", "/rest/vcenter/vm"): "implemented",
    ("GET", "/rest/vcenter/vm/{vm}"): "implemented",
    ("POST", "/rest/vcenter/vm/{vm}/power"): "implemented",
    ("GET", "/rest/vcenter/host"): "implemented",
    ("GET", "/rest/vcenter/datastore"): "implemented",
    ("GET", "/rest/vcenter/network"): "implemented",
    ("GET", "/rest/vcenter/datacenter"): "implemented",
    ("GET", "/rest/vcenter/cluster"): "implemented",
    ("GET", "/rest/appliance/system/version"): "implemented",
}


@lru_cache(maxsize=1)
def _universe_routes() -> dict[tuple[str, str], str]:
    path = Path(__file__).with_name("universe.json")
    if not path.is_file():
        return {}
    payload = json.loads(path.read_text(encoding="utf-8"))
    out: dict[tuple[str, str], str] = {}
    for entry in payload.get("methods") or []:
        verb = str(entry.get("verb", "")).upper()
        route = str(entry.get("path", ""))
        if verb and route:
            out[(verb, route)] = "stub"
    return out


def _merged() -> dict[tuple[str, str], str]:
    merged = dict(_universe_routes())
    merged.update(CORE_IMPLEMENTED)
    return merged


IMPLEMENTED: dict[tuple[str, str], str] = _merged()


def reload_coverage() -> dict[tuple[str, str], str]:
    """Reload universe.json (tests / generator)."""

    _universe_routes.cache_clear()
    IMPLEMENTED.clear()
    IMPLEMENTED.update(_merged())
    from contextlib import suppress

    with suppress(Exception):
        from app.vsphere.contracts.matrix import _compiled_routes

        _compiled_routes.cache_clear()
    return IMPLEMENTED


def catalog_entries() -> list[dict[str, str]]:
    return [
        {"verb": verb, "path": path, "status": status}
        for (verb, path), status in sorted(IMPLEMENTED.items())
    ]


def is_implemented(verb: str, path: str) -> bool:
    return IMPLEMENTED.get((verb.upper(), path)) in ACTIVE_STATUSES


def universe_stats() -> dict[str, int | str]:
    path = Path(__file__).with_name("universe.json")
    if not path.is_file():
        return {"broadcom_operations": 0, "unique_routes": 0}
    payload = json.loads(path.read_text(encoding="utf-8"))
    return {
        "source_label": str(payload.get("source_label") or ""),
        "broadcom_operations": int(payload.get("broadcom_operations") or 0),
        "unique_routes": int(payload.get("unique_routes") or 0),
        "registry_methods": len(IMPLEMENTED),
        "core_methods": len(CORE_IMPLEMENTED),
        "stub_methods": sum(1 for status in IMPLEMENTED.values() if status == "stub"),
    }
