import copy
import json

import pytest

from src.services.ai_agent_post_write_verifier_package import (
    load_latest_ai_agent_post_write_verifier_package,
)


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


def test_load_latest_ai_agent_post_write_verifier_package():
    data = load_latest_ai_agent_post_write_verifier_package()

    assert data["schema_version"] == "ai_agent_post_write_verifier_package_v1"
    assert data["program_status"]["current_task_id"] == "P2-403H"
    assert data["program_status"]["next_task_id"] == "P2-403I"
    assert data["program_status"]["overall_completion_percent"] == 97
    assert data["verifier_truth"]["runtime_write_allowed"] is False
    assert data["verifier_truth"]["post_write_verifier_implemented"] is False
    assert data["verifier_truth"]["post_write_verifier_executed_count"] == 0
    assert data["verifier_truth"]["rollback_work_item_created_count"] == 0
    assert data["rollups"]["verification_target_count"] == len(data["verification_targets"])
    assert data["rollups"]["failure_lane_count"] == len(data["failure_lanes"])
    assert data["rollups"]["operator_action_count"] == len(data["operator_actions"])
    assert data["rollups"]["live_verifier_execution_count"] == 0


def test_rejects_post_write_verifier_implemented(tmp_path):
    data = load_latest_ai_agent_post_write_verifier_package()
    bad = copy.deepcopy(data)
    bad["verifier_truth"]["post_write_verifier_implemented"] = True
    _write_snapshot(tmp_path, bad)

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


def test_rejects_rollback_count_increment(tmp_path):
    data = load_latest_ai_agent_post_write_verifier_package()
    bad = copy.deepcopy(data)
    bad["verifier_truth"]["rollback_work_item_created_count"] = 1
    _write_snapshot(tmp_path, bad)

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


def test_rejects_missing_required_input(tmp_path):
    data = load_latest_ai_agent_post_write_verifier_package()
    bad = copy.deepcopy(data)
    bad["verifier_package"]["required_inputs"] = [
        field for field in bad["verifier_package"]["required_inputs"] if field != "canonical_readback_query"
    ]
    bad["rollups"]["required_input_count"] = len(bad["verifier_package"]["required_inputs"])
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="missing required inputs"):
        load_latest_ai_agent_post_write_verifier_package(tmp_path)


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

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