import copy
import json

import pytest

from src.services.ai_agent_report_status_board import load_latest_ai_agent_report_status_board


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


def test_load_latest_ai_agent_report_status_board():
    data = load_latest_ai_agent_report_status_board()

    assert data["schema_version"] == "ai_agent_report_status_board_v1"
    assert data["program_status"]["current_task_id"] == "P2-108"
    assert data["program_status"]["next_task_id"] == "P2-109"
    assert data["program_status"]["overall_completion_percent"] == 100
    assert data["report_completion_truth"]["daily_report_visible"] is True
    assert data["report_completion_truth"]["weekly_report_visible"] is True
    assert data["report_completion_truth"]["monthly_report_visible"] is True
    assert data["report_completion_truth"]["per_agent_status_visible"] is True
    assert data["report_completion_truth"]["live_report_delivery_enabled"] is False
    assert data["report_completion_truth"]["live_telegram_send_count_24h"] == 0
    assert data["report_completion_truth"]["medium_low_auto_optimization_enabled"] is True
    assert data["report_completion_truth"]["low_medium_high_controlled_apply_allowed"] is True
    assert data["report_completion_truth"]["high_risk_human_approval_required"] is False
    assert data["report_completion_truth"]["critical_break_glass_required"] is True
    assert data["rollups"]["report_card_count"] == 3
    assert data["rollups"]["agent_status_count"] == 3
    assert data["rollups"]["visible_chart_count"] == 3
    assert data["rollups"]["operator_answer_count"] == 4
    assert data["rollups"]["completed_report_count"] == 3
    assert data["rollups"]["workload_unit_total"] == 91
    assert data["rollups"]["workload_done_total"] == 79
    assert data["rollups"]["workload_waiting_approval_total"] == 0
    assert data["rollups"]["workload_controlled_queue_total"] == 12
    assert data["rollups"]["live_delivery_count"] == 0
    assert data["rollups"]["live_runtime_work_units"] == 0
    assert data["rollups"]["live_auto_optimization_count"] == 0


def test_rejects_missing_monthly_card(tmp_path):
    data = load_latest_ai_agent_report_status_board()
    bad = copy.deepcopy(data)
    bad["report_status_cards"] = [
        card for card in bad["report_status_cards"] if card["cadence_id"] != "monthly"
    ]
    bad["rollups"]["report_card_count"] = len(bad["report_status_cards"])
    bad["rollups"]["completed_report_count"] = len(bad["report_status_cards"])
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="daily, weekly, monthly"):
        load_latest_ai_agent_report_status_board(tmp_path)


def test_rejects_missing_agent_status(tmp_path):
    data = load_latest_ai_agent_report_status_board()
    bad = copy.deepcopy(data)
    bad["agent_status_reports"] = [
        agent for agent in bad["agent_status_reports"] if agent["agent_id"] != "nemotron"
    ]
    bad["rollups"]["agent_status_count"] = len(bad["agent_status_reports"])
    bad["rollups"]["workload_unit_total"] = sum(
        agent["work_units_total"] for agent in bad["agent_status_reports"]
    )
    bad["rollups"]["workload_done_total"] = sum(
        agent["work_units_done"] for agent in bad["agent_status_reports"]
    )
    bad["rollups"]["workload_waiting_approval_total"] = sum(
        agent["work_units_waiting_approval"] for agent in bad["agent_status_reports"]
    )
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="OpenClaw, Hermes, NemoTron"):
        load_latest_ai_agent_report_status_board(tmp_path)


def test_rejects_live_telegram_send(tmp_path):
    data = load_latest_ai_agent_report_status_board()
    bad = copy.deepcopy(data)
    bad["report_completion_truth"]["live_telegram_send_count_24h"] = 1
    bad["rollups"]["live_telegram_send_count"] = 1
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="live report counters"):
        load_latest_ai_agent_report_status_board(tmp_path)


def test_rejects_scheduler_enabled(tmp_path):
    data = load_latest_ai_agent_report_status_board()
    bad = copy.deepcopy(data)
    bad["activation_boundaries"]["scheduler_enabled"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="activation boundaries"):
        load_latest_ai_agent_report_status_board(tmp_path)


def test_rejects_forbidden_display_terms(tmp_path):
    data = load_latest_ai_agent_report_status_board()
    bad = copy.deepcopy(data)
    bad["operator_answer_cards"][0]["answer"] = "請把工作視窗逐字稿顯示出來"
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="forbidden display terms"):
        load_latest_ai_agent_report_status_board(tmp_path)


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

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