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_inventory_snapshot_endpoint_returns_committed_snapshot():
    app = FastAPI()
    app.include_router(router, prefix="/api/v1")
    client = TestClient(app)

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

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "ai_agent_automation_inventory_snapshot_v1"
    assert data["program_status"]["overall_completion_percent"] == 100
    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["task_approval_boundary_rollup"]["total_tasks"] == len(data["tasks"]) == 33
    assert data["task_approval_boundary_rollup"]["by_mode"]["read_only_allowed"] == 30
    assert data["task_approval_boundary_rollup"]["by_mode"]["production_change_blocked"] == 1
    assert data["task_approval_boundary_rollup"]["tasks_requiring_explicit_approval"] == [
        "P0-001",
        "P0-004",
        "P1-004",
    ]
    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(asset["asset_id"] == "nemotron_candidate" for asset in data["assets"])
    p1_001 = next(task for task in data["tasks"] if task["task_id"] == "P1-001")
    assert p1_001["status"] == "done"
    assert p1_001["approval_boundary"]["mode"] == "read_only_allowed"
    p1_002 = next(task for task in data["tasks"] if task["task_id"] == "P1-002")
    assert p1_002["status"] == "done"
    assert p1_002["approval_boundary"]["mode"] == "read_only_allowed"
    assert "gitea_workflow_runner_health_2026-06-05.json" in p1_002["output"]
    p1_003 = next(task for task in data["tasks"] if task["task_id"] == "P1-003")
    assert p1_003["status"] == "done"
    assert p1_003["approval_boundary"]["mode"] == "read_only_allowed"
    assert "observability_contract_matrix_2026-06-05.json" in p1_003["output"]
    p1_004 = next(task for task in data["tasks"] if task["task_id"] == "P1-004")
    assert p1_004["status"] == "done"
    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["output"]
    p1_005 = next(task for task in data["tasks"] if task["task_id"] == "P1-005")
    assert p1_005["status"] == "done"
    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["output"]
    p1_006 = next(task for task in data["tasks"] if task["task_id"] == "P1-006")
    assert p1_006["status"] == "done"
    assert p1_006["approval_boundary"]["mode"] == "read_only_allowed"
    assert "live_probe" in p1_006["approval_boundary"]["blocked_actions"]
    assert "service health evidence cards" in p1_006["output"]
    p1_007 = next(task for task in data["tasks"] if task["task_id"] == "P1-007")
    assert p1_007["status"] == "done"
    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["output"]
    assert any(task["task_id"] == "P1-204" for task in data["tasks"])
    assert any(task["task_id"] == "P1-205" for task in data["tasks"])
    assert any(task["task_id"] == "P1-206" for task in data["tasks"])
    assert any(task["task_id"] == "P1-103" for task in data["tasks"])
    assert any(task["task_id"] == "P1-104" for task in data["tasks"])
    assert any(task["task_id"] == "P1-105" for task in data["tasks"])
    assert any(task["task_id"] == "P1-106" for task in data["tasks"])
    p1_305 = next(task for task in data["tasks"] if task["task_id"] == "P1-305")
    assert p1_305["approval_boundary"]["mode"] == "read_only_allowed"
    assert "production_write" in p1_305["approval_boundary"]["blocked_actions"]
    p1_306 = next(task for task in data["tasks"] if task["task_id"] == "P1-306")
    assert p1_306["approval_boundary"]["mode"] == "read_only_allowed"
    assert "runtime_execution" in p1_306["approval_boundary"]["blocked_actions"]
    assert any(evidence["evidence_id"] == "dependency_risk_policy_api" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "dependency_drift_check_plan_api" for evidence in data["evidence"])
    assert any(
        evidence["evidence_id"] == "dependency_upgrade_approval_package_template_api"
        for evidence in data["evidence"]
    )
    assert any(evidence["evidence_id"] == "backup_notification_policy_api" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "backup_dr_evidence_ui" for evidence in data["evidence"])
    assert any(
        evidence["evidence_id"] == "backup_restore_drill_approval_package_template_api"
        for evidence in data["evidence"]
    )
    assert any(evidence["evidence_id"] == "offsite_escrow_readiness_status_api" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "task_approval_boundary_ui" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "backlog_progress_summary_ui" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "runtime_surface_inventory_api" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "gitea_workflow_runner_health_api" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "observability_contract_matrix_api" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "ai_provider_route_matrix_api" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "service_health_gap_matrix_api" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "service_health_evidence_cards_ui" for evidence in data["evidence"])
    assert any(evidence["evidence_id"] == "service_health_failure_notification_policy_api" for evidence in data["evidence"])
