import copy
import json

import pytest

from src.services.ai_agent_runtime_verifier_evidence_review import (
    load_latest_ai_agent_runtime_verifier_evidence_review,
)


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


def test_load_latest_ai_agent_runtime_verifier_evidence_review():
    data = load_latest_ai_agent_runtime_verifier_evidence_review()

    assert data["schema_version"] == "ai_agent_runtime_verifier_evidence_review_v1"
    assert data["program_status"]["current_task_id"] == "P2-403I"
    assert data["program_status"]["next_task_id"] == "P2-403J"
    assert data["program_status"]["overall_completion_percent"] == 99
    assert data["review_truth"]["runtime_verifier_implementation_allowed"] is False
    assert data["review_truth"]["post_write_verifier_execution_allowed"] is False
    assert data["review_truth"]["runtime_verifier_executed_count"] == 0
    assert data["review_truth"]["canonical_readback_executed_count"] == 0
    assert data["review_truth"]["rollback_work_item_created_count"] == 0
    assert data["review_truth"]["telegram_failure_receipt_sent_count"] == 0
    assert data["rollups"]["evidence_check_count"] == len(data["evidence_checks"])
    assert data["rollups"]["implementation_review_lane_count"] == len(data["implementation_review_lanes"])
    assert data["rollups"]["operator_action_count"] == len(data["operator_actions"])
    assert data["rollups"]["live_verifier_execution_count"] == 0


def test_rejects_runtime_verifier_implementation_allowed(tmp_path):
    data = load_latest_ai_agent_runtime_verifier_evidence_review()
    bad = copy.deepcopy(data)
    bad["review_truth"]["runtime_verifier_implementation_allowed"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="verifier review runtime flags"):
        load_latest_ai_agent_runtime_verifier_evidence_review(tmp_path)


def test_rejects_live_verifier_execution_count(tmp_path):
    data = load_latest_ai_agent_runtime_verifier_evidence_review()
    bad = copy.deepcopy(data)
    bad["review_truth"]["runtime_verifier_executed_count"] = 1
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="verifier review counts"):
        load_latest_ai_agent_runtime_verifier_evidence_review(tmp_path)


def test_rejects_missing_required_evidence(tmp_path):
    data = load_latest_ai_agent_runtime_verifier_evidence_review()
    bad = copy.deepcopy(data)
    bad["review_package"]["required_evidence"] = [
        field for field in bad["review_package"]["required_evidence"] if field != "canonical_readback_query_plan"
    ]
    bad["rollups"]["required_evidence_count"] = len(bad["review_package"]["required_evidence"])
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="missing required evidence"):
        load_latest_ai_agent_runtime_verifier_evidence_review(tmp_path)


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

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