import copy
import json

import pytest

from src.services.ai_agent_runtime_worker_shadow_gate import (
    load_latest_ai_agent_runtime_worker_shadow_gate,
)


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


def test_load_latest_ai_agent_runtime_worker_shadow_gate():
    data = load_latest_ai_agent_runtime_worker_shadow_gate()

    assert data["schema_version"] == "ai_agent_runtime_worker_shadow_gate_v1"
    assert data["program_status"]["current_task_id"] == "P2-404"
    assert data["program_status"]["next_task_id"] == "P2-101"
    assert data["program_status"]["overall_completion_percent"] == 96
    assert data["shadow_gate_truth"]["shadow_worker_evidence_gate_ready"] is True
    assert data["shadow_gate_truth"]["promotion_from_fixture_readback_ready"] is True
    assert data["shadow_gate_truth"]["no_write_replay_plan_ready"] is True
    assert data["shadow_gate_truth"]["gateway_queue_write_enabled"] is False
    assert data["shadow_gate_truth"]["telegram_send_enabled"] is False
    assert data["shadow_gate_truth"]["telegram_bot_api_call_enabled"] is False
    assert data["shadow_gate_truth"]["shadow_worker_live_enabled"] is False
    assert data["shadow_gate_truth"]["ai_runtime_worker_enabled"] is False
    assert data["shadow_gate_truth"]["medium_low_auto_worker_enabled"] is False
    assert data["shadow_gate_truth"]["production_write_enabled"] is False
    assert data["rollups"]["shadow_candidate_count"] == 5
    assert data["rollups"]["passed_no_write_candidate_count"] == 2
    assert data["rollups"]["blocked_candidate_count"] == 2
    assert data["rollups"]["needs_owner_review_candidate_count"] == 1
    assert data["rollups"]["no_write_replay_count"] == 4
    assert data["rollups"]["passed_no_write_replay_count"] == 2
    assert data["rollups"]["verifier_shadow_case_count"] == 4
    assert data["rollups"]["agent_role_count"] == 3
    assert data["rollups"]["operator_checkpoint_count"] == 6
    assert len(data["rollups"]["approval_required_checkpoint_ids"]) == 5
    assert data["rollups"]["shadow_worker_live_run_count"] == 0
    assert data["rollups"]["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_runtime_worker_shadow_gate()
    bad = copy.deepcopy(data)
    bad["shadow_gate_truth"]["telegram_send_enabled"] = True
    _write_snapshot(tmp_path, bad)

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


def test_rejects_shadow_worker_live_count(tmp_path):
    data = load_latest_ai_agent_runtime_worker_shadow_gate()
    bad = copy.deepcopy(data)
    bad["shadow_gate_truth"]["shadow_worker_live_run_count_24h"] = 1
    bad["rollups"]["shadow_worker_live_run_count"] = 1
    _write_snapshot(tmp_path, bad)

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


def test_rejects_candidate_secret_read(tmp_path):
    data = load_latest_ai_agent_runtime_worker_shadow_gate()
    bad = copy.deepcopy(data)
    bad["shadow_worker_candidates"][0]["reads_secret_value"] = True
    _write_snapshot(tmp_path, bad)

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


def test_rejects_replay_side_effect(tmp_path):
    data = load_latest_ai_agent_runtime_worker_shadow_gate()
    bad = copy.deepcopy(data)
    bad["no_write_execution_replays"][0]["production_side_effect_count"] = 1
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="production_side_effect_count"):
        load_latest_ai_agent_runtime_worker_shadow_gate(tmp_path)


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

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


def test_rejects_raw_shadow_payload_display(tmp_path):
    data = load_latest_ai_agent_runtime_worker_shadow_gate()
    bad = copy.deepcopy(data)
    bad["display_redaction_contract"]["raw_shadow_payload_display_allowed"] = True
    _write_snapshot(tmp_path, bad)

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


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

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