from __future__ import annotations

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router


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

    response = client.get("/api/v1/agents/agent-high-risk-owner-review-queue")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "ai_agent_high_risk_owner_review_queue_v1"
    assert data["program_status"]["current_task_id"] == "P2-409"
    assert data["program_status"]["next_task_id"] == "P2-410"
    assert data["program_status"]["read_only_mode"] is True
    assert (
        data["program_status"]["runtime_authority"]
        == "controlled_apply_break_glass_queue_readback_no_live_execution"
    )
    assert data["queue_truth"]["all_high_risk_actions_paused"] is False
    assert data["queue_truth"]["high_risk_owner_review_required"] is False
    assert data["queue_truth"]["high_risk_controlled_apply_enabled"] is True
    assert data["queue_truth"]["critical_break_glass_required"] is True
    assert data["rollups"]["source_readback_count"] == len(data["source_readbacks"]) == 6
    assert data["rollups"]["queue_item_count"] == len(data["owner_review_queue_items"]) == 7
    assert data["rollups"]["high_risk_queue_count"] == 5
    assert data["rollups"]["critical_queue_count"] == 2
    assert data["rollups"]["approval_packet_count"] == len(data["approval_packets"]) == 7
    assert data["rollups"]["rejection_guard_count"] == len(data["rejection_guards"]) == 8
    assert data["rollups"]["reviewer_checklist_count"] == len(data["reviewer_checklists"]) == 7
    assert data["rollups"]["approval_packet_required_count"] == 7
    assert data["rollups"]["rollback_owner_required_count"] == 7
    assert data["rollups"]["postcheck_required_count"] == 7
    assert data["rollups"]["blocked_runtime_action_count"] == 42
    assert data["rollups"]["controlled_apply_queue_count"] == 5
    assert data["rollups"]["critical_break_glass_queue_count"] == 2
    assert data["rollups"]["owner_response_required_count"] == 2
    assert data["rollups"]["high_risk_owner_review_required_count"] == 0
    assert data["rollups"]["owner_response_received_count"] == 0
    assert data["rollups"]["owner_response_accepted_count"] == 0
    assert data["rollups"]["auto_worker_run_count"] == 0
    assert data["rollups"]["live_execution_count"] == 0
    assert data["rollups"]["gateway_queue_write_count"] == 0
    assert data["rollups"]["telegram_send_count"] == 0
    assert data["rollups"]["bot_api_call_count"] == 0
    assert data["rollups"]["receipt_production_write_count"] == 0
    assert data["rollups"]["production_write_count"] == 0
    assert data["rollups"]["secret_read_count"] == 0
    assert data["rollups"]["paid_api_call_count"] == 0
    assert data["rollups"]["host_write_count"] == 0
    assert data["rollups"]["kubectl_action_count"] == 0
    assert data["telegram_policy"]["canonical_room"] == "AwoooI SRE 戰情室"
    assert data["telegram_policy"]["canonical_room_env"] == "SRE_GROUP_CHAT_ID"
    assert data["telegram_policy"]["telegram_send_allowed"] is False
    assert data["telegram_policy"]["gateway_queue_write_allowed"] is False
    assert data["telegram_policy"]["direct_bot_api_allowed"] is False
    assert data["routing_policy"]["high_risk_default_route"] == "controlled_apply_queue"
    assert data["routing_policy"]["critical_risk_default_route"] == "critical_break_glass_queue"
    assert data["routing_policy"]["owner_response_required"] is False
    assert data["routing_policy"]["verbal_approval_accepted"] is False
    assert all(
        item["owner_response_required"] is False
        for item in data["owner_review_queue_items"]
        if item["risk_tier"] == "high"
    )
    assert all(
        item["owner_response_required"] is True
        for item in data["owner_review_queue_items"]
        if item["risk_tier"] == "critical"
    )
    assert data["activation_boundaries"]["live_execution_enabled"] is False
    assert data["activation_boundaries"]["openclaw_replacement_allowed"] is False
