import copy
import json

import pytest

from src.services.ai_agent_report_runtime_fixture_readback import (
    load_latest_ai_agent_report_runtime_fixture_readback,
)


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


def test_load_latest_ai_agent_report_runtime_fixture_readback():
    data = load_latest_ai_agent_report_runtime_fixture_readback()

    assert data["schema_version"] == "ai_agent_report_runtime_fixture_readback_v1"
    assert data["program_status"]["current_task_id"] == "P2-403N"
    assert data["program_status"]["next_task_id"] == "P2-404"
    assert data["program_status"]["overall_completion_percent"] == 94
    assert data["fixture_readback_truth"]["fixture_smoke_package_ready"] is True
    assert data["fixture_readback_truth"]["telegram_queue_preview_readback_ready"] is True
    assert data["fixture_readback_truth"]["readback_verifier_dry_run_ready"] is True
    assert data["fixture_readback_truth"]["production_delivery_enabled"] is False
    assert data["fixture_readback_truth"]["telegram_gateway_queue_write_enabled"] is False
    assert data["fixture_readback_truth"]["telegram_send_enabled"] is False
    assert data["fixture_readback_truth"]["telegram_bot_api_call_enabled"] is False
    assert data["fixture_readback_truth"]["ai_runtime_worker_enabled"] is False
    assert data["fixture_readback_truth"]["medium_low_auto_worker_enabled"] is False
    assert data["fixture_readback_truth"]["post_action_verifier_live_readback_enabled"] is False
    assert data["rollups"]["fixture_smoke_count"] == 5
    assert data["rollups"]["passed_fixture_smoke_count"] == 3
    assert data["rollups"]["queue_preview_readback_count"] == 3
    assert data["rollups"]["verifier_dry_run_case_count"] == 4
    assert data["rollups"]["agent_role_count"] == 3
    assert data["rollups"]["operator_checkpoint_count"] == 5
    assert len(data["rollups"]["approval_required_checkpoint_ids"]) == 4
    assert data["rollups"]["live_report_delivery_count"] == 0
    assert data["rollups"]["telegram_gateway_queue_write_count"] == 0
    assert data["rollups"]["telegram_send_count"] == 0
    assert data["rollups"]["telegram_bot_api_call_count"] == 0
    assert data["rollups"]["production_write_count"] == 0


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

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


def test_rejects_gateway_queue_write_count(tmp_path):
    data = load_latest_ai_agent_report_runtime_fixture_readback()
    bad = copy.deepcopy(data)
    bad["fixture_readback_truth"]["telegram_gateway_queue_write_count_24h"] = 1
    bad["rollups"]["telegram_gateway_queue_write_count"] = 1
    _write_snapshot(tmp_path, bad)

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


def test_rejects_fixture_secret_read(tmp_path):
    data = load_latest_ai_agent_report_runtime_fixture_readback()
    bad = copy.deepcopy(data)
    bad["fixture_smoke_results"][0]["reads_secret_value"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="secret value"):
        load_latest_ai_agent_report_runtime_fixture_readback(tmp_path)


def test_rejects_queue_direct_bot_api(tmp_path):
    data = load_latest_ai_agent_report_runtime_fixture_readback()
    bad = copy.deepcopy(data)
    bad["queue_preview_readbacks"][0]["direct_bot_api_allowed"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="direct Bot API"):
        load_latest_ai_agent_report_runtime_fixture_readback(tmp_path)


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

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


def test_rejects_work_window_transcript_display(tmp_path):
    data = load_latest_ai_agent_report_runtime_fixture_readback()
    bad = copy.deepcopy(data)
    bad["display_redaction_contract"]["work_window_transcript_display_allowed"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="display redaction"):
        load_latest_ai_agent_report_runtime_fixture_readback(tmp_path)


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

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