import copy
import json

import pytest

from src.services.ai_agent_report_runtime_dry_run import (
    load_latest_ai_agent_report_runtime_dry_run,
)


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


def test_load_latest_ai_agent_report_runtime_dry_run():
    data = load_latest_ai_agent_report_runtime_dry_run()

    assert data["schema_version"] == "ai_agent_report_runtime_dry_run_v1"
    assert data["program_status"]["current_task_id"] == "P2-403M"
    assert data["program_status"]["next_task_id"] == "P2-403N"
    assert data["program_status"]["overall_completion_percent"] == 91
    assert data["dry_run_truth"]["no_write_dry_run_package_ready"] is True
    assert data["dry_run_truth"]["telegram_gateway_queue_draft_ready"] is True
    assert data["dry_run_truth"]["readback_verifier_plan_ready"] is True
    assert data["dry_run_truth"]["production_delivery_enabled"] is False
    assert data["dry_run_truth"]["telegram_gateway_queue_write_enabled"] is False
    assert data["dry_run_truth"]["telegram_bot_api_call_enabled"] is False
    assert data["dry_run_truth"]["ai_runtime_worker_enabled"] is False
    assert data["dry_run_truth"]["medium_low_auto_worker_enabled"] is False
    assert data["dry_run_truth"]["post_action_verifier_live_readback_enabled"] is False
    assert data["rollups"]["dry_run_artifact_count"] == 5
    assert data["rollups"]["gateway_queue_draft_count"] == 3
    assert data["rollups"]["readback_verifier_case_count"] == 4
    assert data["rollups"]["agent_role_count"] == 3
    assert data["rollups"]["operator_checkpoint_count"] == 6
    assert data["rollups"]["approval_required_checkpoint_ids"] == []
    assert data["rollups"]["live_report_delivery_count"] == 0
    assert data["rollups"]["telegram_gateway_queue_write_count"] == 0
    assert data["rollups"]["telegram_bot_api_call_count"] == 0
    assert data["rollups"]["production_write_count"] == 0


def test_rejects_gateway_queue_write_enabled(tmp_path):
    data = load_latest_ai_agent_report_runtime_dry_run()
    bad = copy.deepcopy(data)
    bad["dry_run_truth"]["telegram_gateway_queue_write_enabled"] = True
    _write_snapshot(tmp_path, bad)

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


def test_rejects_telegram_bot_api_call_count(tmp_path):
    data = load_latest_ai_agent_report_runtime_dry_run()
    bad = copy.deepcopy(data)
    bad["dry_run_truth"]["telegram_bot_api_call_count_24h"] = 1
    bad["rollups"]["telegram_bot_api_call_count"] = 1
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="live write/send/runtime counts"):
        load_latest_ai_agent_report_runtime_dry_run(tmp_path)


def test_rejects_queue_draft_secret_payload(tmp_path):
    data = load_latest_ai_agent_report_runtime_dry_run()
    bad = copy.deepcopy(data)
    bad["telegram_gateway_queue_drafts"][0]["payload_contains_secret"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="secret payload"):
        load_latest_ai_agent_report_runtime_dry_run(tmp_path)


def test_rejects_verifier_live_readback(tmp_path):
    data = load_latest_ai_agent_report_runtime_dry_run()
    bad = copy.deepcopy(data)
    bad["readback_verifier_cases"][0]["live_readback_enabled"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="live readback"):
        load_latest_ai_agent_report_runtime_dry_run(tmp_path)


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

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