from __future__ import annotations

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router


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

    response = client.get("/api/v1/agents/automation-backlog-snapshot")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "ai_agent_automation_backlog_v1"
    assert data["program_status"]["overall_completion_percent"] == 92
    assert data["program_status"]["read_only_mode"] is True
    assert data["program_status"]["current_task_id"] == "P1-007"
    assert data["program_status"]["next_task_id"] == "P2-004"
    assert data["rollups"]["total_items"] == len(data["backlog_items"]) == 25
    assert data["rollups"]["by_priority"]["P1"] == 23
    assert data["rollups"]["by_status"]["done"] == 23
    assert data["rollups"]["by_gate_status"]["read_only_allowed"] == 22
    assert data["progress_summary"]["overall_percent"] == 92
    assert data["progress_summary"]["done_items"] == 23
    assert data["progress_summary"]["total_items"] == 25
    assert data["item_approval_boundary_rollup"]["total_items"] == 25
    assert data["item_approval_boundary_rollup"]["items_requiring_explicit_approval"] == [
        "AUTO-P1-004",
        "AUTO-P2-004",
        "AUTO-P3-001",
    ]
    assert data["approval_boundaries"]["sdk_installation_allowed"] is False
    assert data["approval_boundaries"]["paid_api_call_allowed"] is False
    assert data["approval_boundaries"]["production_routing_allowed"] is False
    assert any(item["item_id"] == "AUTO-P1-204" for item in data["backlog_items"])
    assert any(item["item_id"] == "AUTO-P1-205" for item in data["backlog_items"])
    assert any(item["item_id"] == "AUTO-P1-206" for item in data["backlog_items"])
    assert any(item["item_id"] == "AUTO-P1-103" for item in data["backlog_items"])
    assert any(item["item_id"] == "AUTO-P1-104" for item in data["backlog_items"])
    assert any(item["item_id"] == "AUTO-P1-105" for item in data["backlog_items"])
    assert any(item["item_id"] == "AUTO-P1-106" for item in data["backlog_items"])
    assert any(item["item_id"] == "AUTO-P1-305" for item in data["backlog_items"])
    p1_001 = next(item for item in data["backlog_items"] if item["item_id"] == "AUTO-P1-001")
    assert p1_001["status"] == "done"
    assert "runtime_surface_inventory_2026-06-05.json" in p1_001["evidence_refs"][0]
    p1_002 = next(item for item in data["backlog_items"] if item["item_id"] == "AUTO-P1-002")
    assert p1_002["status"] == "done"
    assert p1_002["next_review"] == "P1-003"
    assert "gitea_workflow_runner_health_2026-06-05.json" in p1_002["evidence_refs"][0]
    p1_003 = next(item for item in data["backlog_items"] if item["item_id"] == "AUTO-P1-003")
    assert p1_003["status"] == "done"
    assert p1_003["next_review"] == "P1-004"
    assert "observability_contract_matrix_2026-06-05.json" in p1_003["evidence_refs"][0]
    p1_004 = next(item for item in data["backlog_items"] if item["item_id"] == "AUTO-P1-004")
    assert p1_004["status"] == "done"
    assert p1_004["next_review"] == "P1-005"
    assert p1_004["approval_boundary"]["mode"] == "production_change_blocked"
    assert "provider_switch" in p1_004["approval_boundary"]["blocked_actions"]
    assert "ai_provider_route_matrix_2026-06-05.json" in p1_004["evidence_refs"][0]
    p1_005 = next(item for item in data["backlog_items"] if item["item_id"] == "AUTO-P1-005")
    assert p1_005["status"] == "done"
    assert p1_005["next_review"] == "P1-006"
    assert p1_005["approval_boundary"]["mode"] == "read_only_allowed"
    assert "service_restart" in p1_005["approval_boundary"]["blocked_actions"]
    assert "service_health_gap_matrix_2026-06-05.json" in p1_005["evidence_refs"][0]
    p1_006 = next(item for item in data["backlog_items"] if item["item_id"] == "AUTO-P1-006")
    assert p1_006["status"] == "done"
    assert p1_006["next_review"] == "P1-007"
    assert p1_006["approval_boundary"]["mode"] == "read_only_allowed"
    assert "live_probe" in p1_006["approval_boundary"]["blocked_actions"]
    assert "/zh-TW/governance?tab=automation-inventory" in p1_006["evidence_refs"]
    p1_007 = next(item for item in data["backlog_items"] if item["item_id"] == "AUTO-P1-007")
    assert p1_007["status"] == "done"
    assert p1_007["next_review"] == "P2-004"
    assert p1_007["approval_boundary"]["mode"] == "read_only_allowed"
    assert "notification_send" in p1_007["approval_boundary"]["blocked_actions"]
    assert "service_health_failure_notification_policy_2026-06-05.json" in p1_007["evidence_refs"][1]
    p1_306 = next(item for item in data["backlog_items"] if item["item_id"] == "AUTO-P1-306")
    assert p1_306["approval_boundary"]["mode"] == "read_only_allowed"
    assert "runtime_execution" in p1_306["approval_boundary"]["blocked_actions"]
    assert any(item["item_id"] == "AUTO-P3-001" for item in data["backlog_items"])
