"""Coverage tests for route_registry.py targeting ~4 missing statements.

Uncovered functions/branches:
- get_routes_by_blueprint: blueprint not found returns []
- find_route: pattern matching
- get_all_routes: prefix handling when prefix is None
- ROUTE_REGISTRY constant structure and blueprint metadata
- endpoint format (blueprint.endpoint)
- prefix concatenation correctness
"""

from local_deep_research.web.routes.route_registry import (
    ROUTE_REGISTRY,
    find_route,
    get_all_routes,
    get_routes_by_blueprint,
)

MODULE = "local_deep_research.web.routes.route_registry"


class TestGetRoutesByBlueprint:
    """Tests for get_routes_by_blueprint."""

    def test_unknown_blueprint_returns_empty(self):
        """Non-existent blueprint returns empty list."""
        assert get_routes_by_blueprint("nonexistent_bp") == []

    def test_known_blueprint_returns_routes(self):
        """Known blueprint returns non-empty route list."""
        routes = get_routes_by_blueprint("research")
        assert len(routes) > 0
        assert all("method" in r for r in routes)
        assert all("path" in r for r in routes)


class TestGetAllRoutes:
    """Tests for get_all_routes."""

    def test_returns_routes_from_all_blueprints(self):
        """Returns routes from every registered blueprint."""
        routes = get_all_routes()
        blueprints = {r["blueprint"] for r in routes}
        assert len(blueprints) == len(ROUTE_REGISTRY)

    def test_prefix_none_produces_root_paths(self):
        """Blueprints with url_prefix=None produce root-level paths."""
        routes = get_all_routes()
        research_routes = [r for r in routes if r["blueprint"] == "research"]
        assert any(r["path"] == "/" for r in research_routes)


class TestFindRoute:
    """Tests for find_route."""

    def test_find_existing_pattern(self):
        """Matching pattern returns results."""
        results = find_route("/api/start")
        assert len(results) > 0

    def test_find_nonexistent_pattern(self):
        """Non-matching pattern returns empty list."""
        results = find_route("/zzz_nonexistent_route_zzz")
        assert results == []

    def test_case_insensitive(self):
        """Search is case-insensitive."""
        upper = find_route("/API/HEALTH")
        lower = find_route("/api/health")
        assert len(upper) == len(lower)


# ---------------------------------------------------------------------------
# Additional deep-coverage tests
# ---------------------------------------------------------------------------


class TestRouteRegistryAllBlueprintsPresent:
    """Verify all expected blueprints appear in the registry."""

    EXPECTED = {"research", "api_v1", "history", "settings", "metrics"}

    def test_expected_blueprints_all_registered(self):
        for bp in self.EXPECTED:
            assert bp in ROUTE_REGISTRY, f"Blueprint '{bp}' missing"

    def test_each_blueprint_has_nonempty_routes(self):
        for bp_name, bp_info in ROUTE_REGISTRY.items():
            assert len(bp_info["routes"]) > 0, f"'{bp_name}' has no routes"


class TestGetAllRoutesEndpointFormat:
    """Endpoints returned by get_all_routes() must be blueprint-qualified."""

    def test_endpoint_has_dot_separator(self):
        for route in get_all_routes():
            assert "." in route["endpoint"], (
                f"Endpoint '{route['endpoint']}' not in blueprint.name format"
            )

    def test_api_v1_routes_prefixed_correctly(self):
        api_routes = [r for r in get_all_routes() if r["blueprint"] == "api_v1"]
        assert len(api_routes) > 0
        for route in api_routes:
            assert route["path"].startswith("/api/v1")

    def test_settings_routes_prefixed_correctly(self):
        settings_routes = [
            r for r in get_all_routes() if r["blueprint"] == "settings"
        ]
        assert len(settings_routes) > 0
        for route in settings_routes:
            assert route["path"].startswith("/settings")


class TestGetRoutesByBlueprintDeep:
    """Deeper checks for get_routes_by_blueprint."""

    def test_settings_routes_have_all_required_keys(self):
        required = {"method", "path", "endpoint", "description"}
        for route in get_routes_by_blueprint("settings"):
            assert required <= set(route.keys())

    def test_metrics_routes_have_metrics_prefix(self):
        for route in get_routes_by_blueprint("metrics"):
            assert route["path"].startswith("/metrics")

    def test_history_routes_have_history_prefix(self):
        for route in get_routes_by_blueprint("history"):
            assert route["path"].startswith("/history")
