from __future__ import annotations

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router


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

    response = client.get("/api/v1/agents/agent-controlled-executor-handoff")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "ai_agent_controlled_executor_handoff_v1"
    assert data["program_status"]["current_task_id"] == "P2-415"
    assert data["program_status"]["next_task_id"] == "P2-416"
    assert data["program_status"]["read_only_mode"] is True
    assert data["program_status"]["runtime_authority"] == "controlled_executor_handoff_readback_no_live_apply"
    assert data["handoff_truth"]["high_risk_controlled_executor_handoff_ready"] is True
    assert data["handoff_truth"]["high_risk_owner_review_required"] is False
    assert data["handoff_truth"]["critical_break_glass_required"] is True
    assert data["handoff_truth"]["controlled_executor_dispatch_enabled"] is False
    assert data["rollups"]["source_readback_count"] == len(data["source_readbacks"]) == 8
    assert data["rollups"]["handoff_packet_count"] == len(data["executor_handoff_packets"]) == 7
    assert data["rollups"]["ready_for_controlled_executor_count"] == 5
    assert data["rollups"]["critical_break_glass_count"] == 2
    assert data["rollups"]["high_risk_packet_count"] == 5
    assert data["rollups"]["critical_packet_count"] == 2
    assert data["rollups"]["executor_route_count"] == len(data["executor_routes"]) == 5
    assert data["rollups"]["verifier_binding_count"] == len(data["verifier_bindings"]) == 5
    assert data["rollups"]["learning_writeback_contract_count"] == len(data["learning_writeback_contracts"]) == 3
    assert data["rollups"]["owner_response_required_count"] == 2
    assert data["rollups"]["missing_check_mode_count"] == 0
    assert data["rollups"]["missing_rollback_count"] == 0
    assert data["rollups"]["missing_verifier_count"] == 0
    assert data["rollups"]["missing_telegram_evidence_count"] == 0
    assert data["rollups"]["missing_learning_writeback_count"] == 0
    assert data["rollups"]["controlled_executor_dispatch_count"] == 0
    assert data["rollups"]["live_apply_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"]["km_write_count"] == 0
    assert data["rollups"]["playbook_trust_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["rollups"]["destructive_operation_count"] == 0
    assert all(
        packet["controlled_executor_handoff_allowed"] is True
        for packet in data["executor_handoff_packets"]
        if packet["risk_tier"] == "high"
    )
    assert all(
        packet["owner_response_required"] is False
        for packet in data["executor_handoff_packets"]
        if packet["risk_tier"] == "high"
    )
    assert all(
        packet["handoff_status"] == "critical_break_glass_only"
        for packet in data["executor_handoff_packets"]
        if packet["risk_tier"] == "critical"
    )
    assert data["activation_boundaries"]["controlled_executor_handoff_preview_allowed"] is True
    assert data["activation_boundaries"]["controlled_executor_dispatch_enabled"] is False
    assert data["activation_boundaries"]["live_apply_enabled"] is False
    assert data["display_redaction_contract"]["redaction_required"] is True
    assert data["display_redaction_contract"]["work_window_transcript_display_allowed"] is False
