import copy
import json

import pytest

from src.services.ai_agent_runtime_write_gate_review import (
    load_latest_ai_agent_runtime_write_gate_review,
)


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


def test_load_latest_ai_agent_runtime_write_gate_review():
    data = load_latest_ai_agent_runtime_write_gate_review()

    assert data["schema_version"] == "ai_agent_runtime_write_gate_review_v1"
    assert data["program_status"]["current_task_id"] == "P2-403G"
    assert data["program_status"]["next_task_id"] == "P2-403H"
    assert data["program_status"]["overall_completion_percent"] == 94
    assert data["runtime_write_truth"]["runtime_write_allowed"] is False
    assert data["runtime_write_truth"]["dual_approval_required"] is True
    assert data["runtime_write_truth"]["dual_approval_received_count"] == 0
    assert data["runtime_write_truth"]["dry_run_hash_verified_count"] == 0
    assert data["runtime_write_truth"]["post_write_verifier_pass_count"] == 0
    assert data["rollups"]["write_target_count"] == len(data["write_targets"])
    assert data["rollups"]["approval_gate_count"] == len(data["approval_gates"])
    assert data["rollups"]["live_write_count_total"] == 0


def test_rejects_runtime_write_enabled(tmp_path):
    data = load_latest_ai_agent_runtime_write_gate_review()
    bad = copy.deepcopy(data)
    bad["runtime_write_truth"]["runtime_write_allowed"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="runtime write flags"):
        load_latest_ai_agent_runtime_write_gate_review(tmp_path)


def test_rejects_dual_approval_count_increment(tmp_path):
    data = load_latest_ai_agent_runtime_write_gate_review()
    bad = copy.deepcopy(data)
    bad["runtime_write_truth"]["dual_approval_received_count"] = 1
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="write gate counts"):
        load_latest_ai_agent_runtime_write_gate_review(tmp_path)


def test_rejects_missing_required_review_field(tmp_path):
    data = load_latest_ai_agent_runtime_write_gate_review()
    bad = copy.deepcopy(data)
    bad["write_gate_review"]["required_fields"] = [
        field for field in bad["write_gate_review"]["required_fields"] if field != "dry_run_preview_hash"
    ]
    bad["rollups"]["required_field_count"] = len(bad["write_gate_review"]["required_fields"])
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="missing required fields"):
        load_latest_ai_agent_runtime_write_gate_review(tmp_path)


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

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