import copy
import json

import pytest

from src.services.ai_agent_report_live_delivery_approval_package import (
    load_latest_ai_agent_report_live_delivery_approval_package,
)


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


def test_load_latest_ai_agent_report_live_delivery_approval_package():
    data = load_latest_ai_agent_report_live_delivery_approval_package()

    assert data["schema_version"] == "ai_agent_report_live_delivery_approval_package_v1"
    assert data["program_status"]["current_task_id"] == "P2-111"
    assert data["program_status"]["next_task_id"] == "P2-112"
    assert data["program_status"]["overall_completion_percent"] == 100
    assert data["prior_report_status_board"]["source_schema_version"] == "ai_agent_report_status_board_v1"
    assert data["prior_report_status_board"]["report_card_count"] == 3
    assert data["prior_report_status_board"]["work_units_total"] == 91
    assert data["prior_runtime_review"]["approval_package_schema_version"] == "ai_agent_runtime_readback_approval_package_v1"
    assert data["prior_runtime_review"]["implementation_review_schema_version"] == "ai_agent_runtime_readback_implementation_review_v1"
    assert data["delivery_approval_truth"]["p2_108_report_status_loaded"] is True
    assert data["delivery_approval_truth"]["p2_109_failure_receipt_gate_loaded"] is True
    assert data["delivery_approval_truth"]["p2_110_implementation_review_loaded"] is True
    assert data["delivery_approval_truth"]["delivery_approval_package_ready"] is True
    assert data["delivery_approval_truth"]["sre_war_room_route_locked"] is True
    assert data["delivery_approval_truth"]["scheduler_enabled"] is False
    assert data["delivery_approval_truth"]["gateway_queue_write_enabled"] is False
    assert data["delivery_approval_truth"]["telegram_send_enabled"] is False
    assert data["delivery_approval_truth"]["bot_api_call_enabled"] is False
    assert data["delivery_approval_truth"]["report_receipt_write_enabled"] is False
    assert data["delivery_approval_truth"]["ai_analysis_run_enabled"] is False
    assert data["delivery_approval_truth"]["scheduled_delivery_count_24h"] == 0
    assert data["delivery_approval_truth"]["telegram_send_count_24h"] == 0
    assert data["rollups"]["delivery_approval_packet_count"] == 5
    assert data["rollups"]["route_lock_gate_count"] == 4
    assert data["rollups"]["payload_redaction_check_count"] == 5
    assert data["rollups"]["dry_run_delivery_receipt_count"] == 4
    assert data["rollups"]["operator_action_count"] == 5
    assert data["rollups"]["approval_required_packet_count"] == 3
    assert data["rollups"]["blocked_packet_count"] == 1
    assert data["rollups"]["blocked_route_gate_count"] == 1
    assert data["rollups"]["blocked_receipt_count"] == 1
    assert data["rollups"]["owner_approval_received_count"] == 0
    assert data["rollups"]["gateway_queue_write_count"] == 0
    assert data["rollups"]["telegram_send_count"] == 0
    assert data["rollups"]["bot_api_call_count"] == 0
    assert data["rollups"]["report_receipt_write_count"] == 0
    assert data["rollups"]["ai_analysis_run_count"] == 0
    assert data["rollups"]["auto_optimization_count"] == 0
    assert data["rollups"]["production_write_count"] == 0


def test_rejects_telegram_send_enabled(tmp_path):
    data = load_latest_ai_agent_report_live_delivery_approval_package()
    bad = copy.deepcopy(data)
    bad["delivery_approval_truth"]["telegram_send_enabled"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="live send/write flags"):
        load_latest_ai_agent_report_live_delivery_approval_package(tmp_path)


def test_rejects_gateway_queue_write_count(tmp_path):
    data = load_latest_ai_agent_report_live_delivery_approval_package()
    bad = copy.deepcopy(data)
    bad["delivery_approval_truth"]["gateway_queue_write_count_24h"] = 1
    bad["rollups"]["gateway_queue_write_count"] = 1
    _write_snapshot(tmp_path, bad)

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


def test_rejects_packet_send_mode(tmp_path):
    data = load_latest_ai_agent_report_live_delivery_approval_package()
    bad = copy.deepcopy(data)
    bad["delivery_approval_packets"][0]["no_send_mode"] = False
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="no_send_mode"):
        load_latest_ai_agent_report_live_delivery_approval_package(tmp_path)


def test_rejects_route_gate_send_enabled(tmp_path):
    data = load_latest_ai_agent_report_live_delivery_approval_package()
    bad = copy.deepcopy(data)
    bad["route_lock_gates"][0]["telegram_send_enabled"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="telegram_send_enabled"):
        load_latest_ai_agent_report_live_delivery_approval_package(tmp_path)


def test_rejects_redaction_display_allowed(tmp_path):
    data = load_latest_ai_agent_report_live_delivery_approval_package()
    bad = copy.deepcopy(data)
    bad["payload_redaction_checks"][0]["display_allowed"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="display_allowed"):
        load_latest_ai_agent_report_live_delivery_approval_package(tmp_path)


def test_rejects_receipt_write_allowed(tmp_path):
    data = load_latest_ai_agent_report_live_delivery_approval_package()
    bad = copy.deepcopy(data)
    bad["dry_run_delivery_receipts"][0]["receipt_write_allowed"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="receipt_write_allowed"):
        load_latest_ai_agent_report_live_delivery_approval_package(tmp_path)


def test_rejects_forbidden_display_terms(tmp_path):
    data = load_latest_ai_agent_report_live_delivery_approval_package()
    bad = copy.deepcopy(data)
    bad["operator_actions"][0]["operator_instruction"] = "不得顯示工作視窗內容"
    _write_snapshot(tmp_path, bad)

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


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

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