import copy
import json

import pytest

from src.services.ai_agent_candidate_operation_dry_run_evidence import (
    load_latest_ai_agent_candidate_operation_dry_run_evidence,
)


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


def test_load_latest_ai_agent_candidate_operation_dry_run_evidence():
    data = load_latest_ai_agent_candidate_operation_dry_run_evidence()

    assert data["schema_version"] == "ai_agent_candidate_operation_dry_run_evidence_v1"
    assert data["program_status"]["current_task_id"] == "P2-102"
    assert data["program_status"]["next_task_id"] == "P2-103"
    assert data["program_status"]["overall_completion_percent"] == 98
    assert data["dry_run_truth"]["dry_run_evidence_gate_ready"] is True
    assert data["dry_run_truth"]["all_candidate_operations_have_dry_run_evidence"] is True
    assert data["dry_run_truth"]["runtime_execution_enabled"] is False
    assert data["dry_run_truth"]["gateway_queue_write_enabled"] is False
    assert data["dry_run_truth"]["telegram_send_enabled"] is False
    assert data["dry_run_truth"]["telegram_bot_api_call_enabled"] is False
    assert data["dry_run_truth"]["production_write_enabled"] is False
    assert data["dry_run_truth"]["secret_value_read_enabled"] is False
    assert data["dry_run_truth"]["destructive_operation_enabled"] is False
    assert data["rollups"]["candidate_operation_count"] == 13
    assert data["rollups"]["candidate_with_dry_run_evidence_count"] == 13
    assert data["rollups"]["passed_no_write_count"] == 4
    assert data["rollups"]["needs_owner_review_count"] == 3
    assert data["rollups"]["blocked_until_allowlist_count"] == 2
    assert data["rollups"]["blocked_by_policy_count"] == 4
    assert data["rollups"]["verifier_plan_count"] == 6
    assert data["rollups"]["gate_evidence_requirement_count"] == 7
    assert data["rollups"]["operator_handoff_count"] == 5
    assert data["rollups"]["side_effect_count"] == 0
    assert data["rollups"]["runtime_execution_count"] == 0
    assert data["rollups"]["gateway_queue_write_count"] == 0
    assert data["rollups"]["telegram_send_count"] == 0
    assert data["rollups"]["production_write_count"] == 0
    assert data["rollups"]["secret_value_read_count"] == 0
    assert data["rollups"]["destructive_operation_count"] == 0


def test_rejects_runtime_execution_enabled(tmp_path):
    data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
    bad = copy.deepcopy(data)
    bad["dry_run_truth"]["runtime_execution_enabled"] = True
    _write_snapshot(tmp_path, bad)

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


def test_rejects_gateway_queue_write_count(tmp_path):
    data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
    bad = copy.deepcopy(data)
    bad["dry_run_truth"]["gateway_queue_write_count_24h"] = 1
    bad["rollups"]["gateway_queue_write_count"] = 1
    _write_snapshot(tmp_path, bad)

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


def test_rejects_candidate_side_effect(tmp_path):
    data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
    bad = copy.deepcopy(data)
    bad["candidate_operations"][0]["side_effect_count"] = 1
    bad["rollups"]["side_effect_count"] = 1
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="side-effect counts"):
        load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)


def test_rejects_candidate_without_hash(tmp_path):
    data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
    bad = copy.deepcopy(data)
    bad["candidate_operations"][0]["output_evidence_hash"] = "missing"
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="output_evidence_hash"):
        load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)


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

    with pytest.raises(ValueError, match="live_readback_enabled"):
        load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)


def test_rejects_gate_opening_live_execution(tmp_path):
    data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
    bad = copy.deepcopy(data)
    bad["gate_evidence_requirements"][0]["opens_live_execution"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="opens_live_execution"):
        load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)


def test_rejects_handoff_runtime_action(tmp_path):
    data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
    bad = copy.deepcopy(data)
    bad["operator_handoffs"][0]["creates_runtime_action"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="creates_runtime_action"):
        load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)


def test_rejects_forbidden_display_terms(tmp_path):
    data = load_latest_ai_agent_candidate_operation_dry_run_evidence()
    bad = copy.deepcopy(data)
    bad["verifier_plans"][0]["expected_signal"] = "raw payload / private reasoning 不得顯示"
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="forbidden display terms"):
        load_latest_ai_agent_candidate_operation_dry_run_evidence(tmp_path)


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

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