from __future__ import annotations

import os

from fastapi.testclient import TestClient

os.environ.setdefault("DATABASE_URL", "postgresql+asyncpg://test:test@localhost/test")

from src.main import app


def test_ai_agent_professional_task_expansion_endpoint() -> None:
    client = TestClient(app)

    response = client.get("/api/v1/agents/agent-professional-task-expansion")

    assert response.status_code == 200
    payload = response.json()
    assert payload["schema_version"] == "ai_agent_professional_task_expansion_v1"
    assert payload["program_status"]["current_task_id"] == "P2-405F"
    assert payload["program_status"]["next_task_id"] == "P2-406B"
    assert payload["program_status"]["overall_completion_percent"] == 99
    assert payload["program_status"]["runtime_authority"] == (
        "professional_task_expansion_and_telegram_bridge_read_only_no_send"
    )
    assert payload["telegram_runtime_bridge"]["canonical_room"] == "AwoooI SRE 戰情室"
    assert payload["telegram_runtime_bridge"]["direct_bot_api_allowed"] is False
    assert payload["telegram_runtime_bridge"]["gateway_queue_write_enabled"] is False
    assert payload["telegram_runtime_bridge"]["telegram_send_enabled"] is False
    assert payload["telegram_runtime_bridge"]["bot_api_call_enabled"] is False
    assert payload["rollups"]["professional_task_count"] == 24
    assert payload["rollups"]["domain_count"] == 8
    assert payload["rollups"]["approval_required_count"] == 19
    assert payload["rollups"]["high_risk_task_count"] == 6
    assert payload["rollups"]["critical_risk_task_count"] == 5
    assert payload["rollups"]["gateway_queue_write_count"] == 0
    assert payload["rollups"]["telegram_send_count"] == 0
    assert payload["rollups"]["bot_api_call_count"] == 0
    assert payload["rollups"]["production_write_count"] == 0
    assert payload["rollups"]["no_send_preview_count"] == 6
    assert payload["rollups"]["dedup_key_count"] == 6
    assert payload["rollups"]["receipt_expectation_count"] == 6
    assert payload["rollups"]["canary_approval_package_count"] == 1
    assert payload["rollups"]["preview_send_enabled_count"] == 0
    assert payload["rollups"]["canary_send_approval_packet_count"] == 1
    assert payload["rollups"]["canary_operator_approval_field_count"] == 7
    assert payload["rollups"]["canary_approval_granted_count"] == 0
    assert payload["rollups"]["canary_send_execution_enabled_count"] == 0
    assert payload["rollups"]["canary_gateway_queue_write_enabled_count"] == 0
    assert payload["rollups"]["canary_bot_api_call_enabled_count"] == 0
    assert payload["rollups"]["canary_delivery_gate_count"] == 1
    assert payload["rollups"]["canary_delivery_required_field_count"] == 8
    assert payload["rollups"]["canary_delivery_approved_count"] == 0
    assert payload["rollups"]["canary_delivery_attempt_allowed_count"] == 0
    assert payload["rollups"]["canary_delivery_gateway_queue_write_enabled_count"] == 0
    assert payload["rollups"]["canary_delivery_bot_api_call_enabled_count"] == 0
    assert payload["rollups"]["canary_delivery_rehearsal_count"] == 1
    assert payload["rollups"]["canary_delivery_rehearsal_step_count"] == 6
    assert payload["rollups"]["canary_delivery_rehearsal_readback_check_count"] == 8
    assert payload["rollups"]["canary_delivery_rehearsal_failed_check_count"] == 0
    assert payload["rollups"]["canary_delivery_rehearsal_gateway_queue_write_enabled_count"] == 0
    assert payload["rollups"]["canary_delivery_rehearsal_bot_api_call_enabled_count"] == 0
    assert payload["rollups"]["canary_delivery_rehearsal_receipt_write_enabled_count"] == 0
    assert payload["rollups"]["canary_live_delivery_owner_review_gate_count"] == 1
    assert payload["rollups"]["canary_live_delivery_owner_review_required_field_count"] == 9
    assert payload["rollups"]["canary_live_delivery_owner_review_received_count"] == 0
    assert payload["rollups"]["canary_live_delivery_owner_review_accepted_count"] == 0
    assert payload["rollups"]["canary_live_delivery_approved_count"] == 0
    assert payload["rollups"]["canary_live_delivery_telegram_send_enabled_count"] == 0
    assert payload["telegram_runtime_bridge"]["canary_approval_package"]["live_send_enabled"] is False
    assert payload["telegram_runtime_bridge"]["canary_send_approval_packet"]["approval_granted"] is False
    assert (
        payload["telegram_runtime_bridge"]["canary_send_approval_packet"]["selected_message_type"]
        == "not_selected"
    )
    assert payload["telegram_runtime_bridge"]["canary_delivery_gate"]["delivery_approved"] is False
    assert payload["telegram_runtime_bridge"]["canary_delivery_gate"]["delivery_attempt_allowed"] is False
    assert payload["telegram_runtime_bridge"]["canary_delivery_gate"]["target_room_value_visible"] is False
    assert payload["telegram_runtime_bridge"]["canary_delivery_rehearsal"]["rehearsal_ready"] is True
    assert (
        payload["telegram_runtime_bridge"]["canary_delivery_rehearsal"]["selected_message_type"]
        == "daily_agent_workload_digest"
    )
    assert (
        payload["telegram_runtime_bridge"]["canary_delivery_rehearsal"]["gateway_envelope_preview"][
            "telegram_send_enabled"
        ]
        is False
    )
    owner_gate = payload["telegram_runtime_bridge"]["canary_live_delivery_owner_review_gate"]
    assert owner_gate["gate_ready"] is True
    assert owner_gate["owner_review_received"] is False
    assert owner_gate["owner_review_accepted"] is False
    assert owner_gate["live_canary_delivery_approved"] is False
    assert owner_gate["execution_flags"]["telegram_send_enabled"] is False
    assert len(payload["telegram_runtime_bridge"]["no_send_message_previews"]) == 6
    assert len(payload["telegram_runtime_bridge"]["receipt_expectations"]) == 6
