"""RFC 9727 /.well-known/api-catalog."""
from fastapi.testclient import TestClient
from src.core.api_catalog import (
    LINKSET_MEDIA_TYPE,
    PUBLISHED_APIS,
    RFC9727_PROFILE,
    build_api_catalog,
)
from src.main import app


def test_api_catalog_linkset_shape():
    catalog = build_api_catalog("https://api.example.com")
    assert list(catalog.keys()) == ["linkset"]
    assert len(catalog["linkset"]) == len(PUBLISHED_APIS)
    anchors = {entry["anchor"] for entry in catalog["linkset"]}
    assert anchors == {f"https://api.example.com{api.path}" for api in PUBLISHED_APIS}
    for entry in catalog["linkset"]:
        assert entry["service-desc"][0]["href"] == "https://api.example.com/openapi.json"
        assert entry["service-desc"][0]["type"] == "application/json"
        assert entry["service-doc"][0]["href"] == "https://api.example.com/docs"
        assert entry["service-doc"][0]["type"] == "text/html"
        assert entry["status"][0]["href"] == "https://api.example.com/health"
        assert entry["status"][0]["type"] == "application/json"


def test_well_known_api_catalog_get():
    client = TestClient(app)
    response = client.get("/.well-known/api-catalog")
    assert response.status_code == 200
    assert response.headers["content-type"].startswith("application/linkset+json")
    assert RFC9727_PROFILE in response.headers["content-type"]
    assert 'rel="api-catalog"' in response.headers["link"]
    body = response.json()
    assert isinstance(body["linkset"], list)
    assert len(body["linkset"]) == len(PUBLISHED_APIS)
    for entry in body["linkset"]:
        from src.config import get_settings
        assert entry["anchor"].startswith(get_settings().app_url.rstrip("/"))
        assert "service-desc" in entry
        assert "service-doc" in entry
        assert "status" in entry


def test_well_known_api_catalog_head():
    client = TestClient(app)
    response = client.head("/.well-known/api-catalog")
    assert response.status_code == 200
    assert response.headers["content-type"].startswith("application/linkset+json")
    assert RFC9727_PROFILE in response.headers["content-type"]
    assert 'rel="api-catalog"' in response.headers["link"]
    assert response.content == b""


def test_linkset_media_type_constant():
    assert LINKSET_MEDIA_TYPE == f'application/linkset+json; profile="{RFC9727_PROFILE}"'


def test_public_openapi_excludes_private_operations_and_schemas():
    from src.config import get_settings
    from src.core.api_catalog import PUBLIC_OPERATION_PATHS
    client = TestClient(app, base_url="https://private-core.invalid")
    response = client.get("/public-openapi.json")
    assert response.status_code == 200
    document = response.json()
    assert set(document["paths"]) == PUBLIC_OPERATION_PATHS
    assert document["servers"] == [{"url": get_settings().app_url.rstrip("/")}]
    assert "Private" not in str(document.get("components", {}))
    assert "Admin" not in str(document.get("components", {}))
    assert "private-core.invalid" not in response.text
    assert client.head("/public-openapi.json").content == b""
