import copy
import json

import pytest

from src.services.ai_agent_report_truth_actionability_review import (
    load_latest_ai_agent_report_truth_actionability_review,
)


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


def test_load_latest_ai_agent_report_truth_actionability_review():
    data = load_latest_ai_agent_report_truth_actionability_review()

    assert data["schema_version"] == "ai_agent_report_truth_actionability_review_v1"
    assert data["program_status"]["current_task_id"] == "P2-403J"
    assert data["program_status"]["next_task_id"] == "P2-403K"
    assert data["report_truth"]["all_zero_weekly_report_is_actionable_anomaly"] is True
    assert data["report_truth"]["freshness_gate_implemented"] is False
    assert data["report_truth"]["telegram_report_send_allowed"] is False
    assert data["rollups"]["zero_signal_finding_count"] == len(data["zero_signal_findings"])
    assert data["rollups"]["missing_cadence_contract_count"] == 1
    assert data["telegram_routing_consolidation"]["canonical_room_name"] == "AwoooI SRE 戰情室"
    assert data["telegram_routing_consolidation"]["canonical_room_env"] == "SRE_GROUP_CHAT_ID"
    assert data["telegram_routing_consolidation"]["direct_telegram_api_send_allowed"] is False
    assert data["rollups"]["telegram_route_finding_count"] == len(data["telegram_route_findings"])
    assert data["rollups"]["all_zero_weekly_report_confidence"] == "low_trust_actionable_anomaly"


def test_rejects_all_zero_weekly_report_as_green(tmp_path):
    data = load_latest_ai_agent_report_truth_actionability_review()
    bad = copy.deepcopy(data)
    bad["report_truth"]["all_zero_weekly_report_is_actionable_anomaly"] = False
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="all-zero weekly report"):
        load_latest_ai_agent_report_truth_actionability_review(tmp_path)


def test_rejects_runtime_report_send_allowed(tmp_path):
    data = load_latest_ai_agent_report_truth_actionability_review()
    bad = copy.deepcopy(data)
    bad["report_truth"]["telegram_report_send_allowed"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="telegram_report_send_allowed"):
        load_latest_ai_agent_report_truth_actionability_review(tmp_path)


def test_rejects_missing_heartbeat_noise_lane(tmp_path):
    data = load_latest_ai_agent_report_truth_actionability_review()
    bad = copy.deepcopy(data)
    bad["alert_actionability_lanes"] = [
        lane for lane in bad["alert_actionability_lanes"] if lane["lane_id"] != "heartbeat_noise"
    ]
    bad["rollups"]["actionability_lane_count"] = len(bad["alert_actionability_lanes"])
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="heartbeat_noise"):
        load_latest_ai_agent_report_truth_actionability_review(tmp_path)


def test_rejects_non_canonical_telegram_room(tmp_path):
    data = load_latest_ai_agent_report_truth_actionability_review()
    bad = copy.deepcopy(data)
    bad["telegram_routing_consolidation"]["canonical_room_name"] = "Legacy Ops Group"
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="canonical Telegram room"):
        load_latest_ai_agent_report_truth_actionability_review(tmp_path)


def test_rejects_direct_telegram_api_send_allowed(tmp_path):
    data = load_latest_ai_agent_report_truth_actionability_review()
    bad = copy.deepcopy(data)
    bad["telegram_routing_consolidation"]["direct_telegram_api_send_allowed"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="direct_telegram_api_send_allowed"):
        load_latest_ai_agent_report_truth_actionability_review(tmp_path)


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

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