from __future__ import annotations

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router


def test_ai_provider_route_matrix_endpoint_returns_committed_snapshot():
    app = FastAPI()
    app.include_router(router, prefix="/api/v1")
    client = TestClient(app)

    response = client.get("/api/v1/agents/ai-provider-route-matrix")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "ai_provider_route_matrix_v1"
    assert data["program_status"]["current_task_id"] == "P1-004"
    assert data["program_status"]["next_task_id"] == "P1-005"
    assert data["program_status"]["read_only_mode"] is True
    assert data["rollups"]["total_routes"] == len(data["provider_routes"]) == 7
    assert data["rollups"]["provider_switch_allowed_count"] == 0
    assert data["rollups"]["paid_api_call_allowed_count"] == 0
    assert data["rollups"]["shadow_or_canary_allowed_count"] == 0
    assert data["rollups"]["runtime_route_change_allowed_count"] == 0
    assert data["operation_boundaries"]["provider_switch_allowed"] is False
    assert data["operation_boundaries"]["paid_api_call_allowed"] is False
    assert data["operation_boundaries"]["runtime_execution_allowed"] is False
    assert data["approval_boundaries"]["provider_switch_approved"] is False
    assert data["approval_boundaries"]["cost_change_approved"] is False
    assert any(route["route_id"] == "ollama_global_endpoint_order" for route in data["provider_routes"])
    assert any(route["route_id"] == "alert_ai_ollama_first_lane" for route in data["provider_routes"])
    nemotron = next(
        route for route in data["provider_routes"]
        if route["route_id"] == "nemotron_tool_calling_candidate"
    )
    assert nemotron["status"] == "blocked"
    assert nemotron["route_gate"] == "candidate_blocked"
    assert data["operator_contract"]["display_mode"] == "read_only_ai_provider_route_matrix"
    assert "provider 切換批准" in data["operator_contract"]["must_not_interpret_as"]
