import copy
import json

import pytest

from src.services.ai_agent_report_automation_review import (
    load_latest_ai_agent_report_automation_review,
)


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


def test_load_latest_ai_agent_report_automation_review():
    data = load_latest_ai_agent_report_automation_review()

    assert data["schema_version"] == "ai_agent_report_automation_review_v1"
    assert data["program_status"]["current_task_id"] == "P2-403J"
    assert data["program_status"]["next_task_id"] == "P2-403K"
    assert data["program_status"]["overall_completion_percent"] == 100
    assert data["report_truth"]["daily_report_ready"] is True
    assert data["report_truth"]["weekly_report_ready"] is True
    assert data["report_truth"]["monthly_report_ready"] is True
    assert data["report_truth"]["medium_low_auto_policy_defined"] is True
    assert data["report_truth"]["medium_low_auto_execution_enabled"] is False
    assert data["report_truth"]["high_risk_requires_approval"] is True
    assert data["report_truth"]["live_auto_optimization_count_24h"] == 0
    assert data["rollups"]["report_cadence_count"] == 3
    assert data["rollups"]["agent_count"] == 3
    assert data["rollups"]["chart_count"] == 4
    assert data["rollups"]["recommendation_count"] == 5
    assert data["rollups"]["workload_unit_total"] == 91
    assert data["rollups"]["current_auto_execution_enabled_count"] == 0
    assert data["rollups"]["live_report_delivery_count"] == 0
    assert data["rollups"]["live_auto_optimization_count"] == 0


def test_rejects_missing_monthly_report(tmp_path):
    data = load_latest_ai_agent_report_automation_review()
    bad = copy.deepcopy(data)
    bad["report_cadences"] = [
        cadence for cadence in bad["report_cadences"] if cadence["cadence_id"] != "monthly"
    ]
    bad["rollups"]["report_cadence_count"] = len(bad["report_cadences"])
    _write_snapshot(tmp_path, bad)

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


def test_rejects_medium_low_auto_execution_enabled(tmp_path):
    data = load_latest_ai_agent_report_automation_review()
    bad = copy.deepcopy(data)
    bad["report_truth"]["medium_low_auto_execution_enabled"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="live report automation flags"):
        load_latest_ai_agent_report_automation_review(tmp_path)


def test_rejects_high_risk_approval_disabled(tmp_path):
    data = load_latest_ai_agent_report_automation_review()
    bad = copy.deepcopy(data)
    bad["report_truth"]["high_risk_requires_approval"] = False
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="high risk approval gate"):
        load_latest_ai_agent_report_automation_review(tmp_path)


def test_rejects_report_delivery_count(tmp_path):
    data = load_latest_ai_agent_report_automation_review()
    bad = copy.deepcopy(data)
    bad["report_truth"]["report_delivery_count_24h"] = 1
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="live report / automation counts"):
        load_latest_ai_agent_report_automation_review(tmp_path)


def test_rejects_rollup_mismatch(tmp_path):
    data = load_latest_ai_agent_report_automation_review()
    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_automation_review(tmp_path)
