import copy
import json

import pytest

from src.services.ai_agent_report_runtime_readiness import (
    load_latest_ai_agent_report_runtime_readiness,
)


def _write_snapshot(tmp_path, payload):
    path = tmp_path / "ai_agent_report_runtime_readiness_2026-06-12.json"
    path.write_text(json.dumps(payload), encoding="utf-8")
    return path


def test_load_latest_ai_agent_report_runtime_readiness():
    data = load_latest_ai_agent_report_runtime_readiness()

    assert data["schema_version"] == "ai_agent_report_runtime_readiness_v1"
    assert data["program_status"]["current_task_id"] == "P2-403L"
    assert data["program_status"]["next_task_id"] == "P2-403M"
    assert data["program_status"]["overall_completion_percent"] == 86
    assert data["activation_truth"]["report_scheduler_contract_ready"] is True
    assert data["activation_truth"]["telegram_gateway_queue_contract_ready"] is True
    assert data["activation_truth"]["ai_readback_analysis_contract_ready"] is True
    assert data["activation_truth"]["medium_low_auto_guard_contract_ready"] is True
    assert data["activation_truth"]["live_report_delivery_enabled"] is False
    assert data["activation_truth"]["telegram_gateway_queue_write_enabled"] is False
    assert data["activation_truth"]["medium_low_auto_worker_enabled"] is False
    assert data["activation_truth"]["production_optimization_count_24h"] == 0
    assert data["telegram_route_readiness"]["canonical_room"] == "AwoooI SRE 戰情室"
    assert data["telegram_route_readiness"]["direct_bot_api_allowed"] is False
    assert data["telegram_route_readiness"]["bot_log_out_allowed"] is False
    assert data["rollups"]["runtime_lane_count"] == 7
    assert data["rollups"]["report_cadence_gate_count"] == 3
    assert data["rollups"]["operator_decision_count"] == 7
    assert data["rollups"]["automation_policy_count"] == 4
    assert data["rollups"]["current_enabled_count"] == 0
    assert data["rollups"]["live_report_delivery_count"] == 0
    assert data["rollups"]["telegram_gateway_queue_write_count"] == 0
    assert data["rollups"]["live_medium_low_auto_execution_count"] == 0


def test_rejects_live_report_delivery_enabled(tmp_path):
    data = load_latest_ai_agent_report_runtime_readiness()
    bad = copy.deepcopy(data)
    bad["activation_truth"]["live_report_delivery_enabled"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="live runtime flags"):
        load_latest_ai_agent_report_runtime_readiness(tmp_path)


def test_rejects_telegram_gateway_queue_write_count(tmp_path):
    data = load_latest_ai_agent_report_runtime_readiness()
    bad = copy.deepcopy(data)
    bad["activation_truth"]["telegram_gateway_queue_write_count_24h"] = 1
    bad["rollups"]["telegram_gateway_queue_write_count"] = 1
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="live report runtime counts"):
        load_latest_ai_agent_report_runtime_readiness(tmp_path)


def test_rejects_direct_telegram_api(tmp_path):
    data = load_latest_ai_agent_report_runtime_readiness()
    bad = copy.deepcopy(data)
    bad["telegram_route_readiness"]["direct_bot_api_allowed"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="Telegram live route fields"):
        load_latest_ai_agent_report_runtime_readiness(tmp_path)


def test_rejects_medium_low_policy_without_guard(tmp_path):
    data = load_latest_ai_agent_report_runtime_readiness()
    bad = copy.deepcopy(data)
    for policy in bad["automation_policies"]:
        if policy["risk_id"] == "medium":
            policy["auto_allowed_after_guard"] = False
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="medium"):
        load_latest_ai_agent_report_runtime_readiness(tmp_path)


def test_rejects_high_risk_auto_allowed(tmp_path):
    data = load_latest_ai_agent_report_runtime_readiness()
    bad = copy.deepcopy(data)
    for policy in bad["automation_policies"]:
        if policy["risk_id"] == "high":
            policy["auto_allowed_after_guard"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="high"):
        load_latest_ai_agent_report_runtime_readiness(tmp_path)


def test_rejects_rollup_mismatch(tmp_path):
    data = load_latest_ai_agent_report_runtime_readiness()
    bad = copy.deepcopy(data)
    bad["rollups"]["runtime_lane_count"] = 999
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="rollup counts"):
        load_latest_ai_agent_report_runtime_readiness(tmp_path)
