import copy
import json

import pytest

from src.services.ai_agent_owner_approved_result_capture_readback import (
    load_latest_ai_agent_owner_approved_result_capture_readback,
)


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


def test_load_latest_ai_agent_owner_approved_result_capture_readback():
    data = load_latest_ai_agent_owner_approved_result_capture_readback()

    assert data["schema_version"] == "ai_agent_owner_approved_result_capture_readback_v1"
    assert data["program_status"]["current_task_id"] == "P2-107"
    assert data["program_status"]["next_task_id"] == "P2-108"
    assert data["program_status"]["overall_completion_percent"] == 100
    assert data["prior_dry_run_readback"]["source_schema_version"] == "ai_agent_owner_approved_result_capture_dry_run_v1"
    assert data["prior_dry_run_readback"]["result_capture_template_count"] == 5
    assert data["prior_dry_run_readback"]["approved_without_execution_meta_24h"] == 63
    assert data["readback_truth"]["p2_106_dry_run_loaded"] is True
    assert data["readback_truth"]["fixture_readback_allowed"] is True
    assert data["readback_truth"]["readback_digest_ready"] is True
    assert data["readback_truth"]["canonical_runtime_readback_enabled"] is False
    assert data["readback_truth"]["reviewer_queue_write_enabled"] is False
    assert data["readback_truth"]["owner_approval_received_count"] == 0
    assert data["readback_truth"]["readback_digest_generated_count"] == 0
    assert data["readback_truth"]["promotion_approved_count"] == 0
    assert data["rollups"]["readback_digest_count"] == 5
    assert data["rollups"]["promotion_review_count"] == 5
    assert data["rollups"]["failure_lane_count"] == 4
    assert data["rollups"]["reviewer_queue_preview_count"] == 4
    assert data["rollups"]["operator_action_count"] == 5
    assert data["rollups"]["reviewer_queue_write_count"] == 0
    assert data["rollups"]["result_capture_write_count"] == 0
    assert data["rollups"]["score_write_count"] == 0
    assert data["rollups"]["learning_write_count"] == 0
    assert data["rollups"]["playbook_trust_write_count"] == 0
    assert data["rollups"]["telegram_send_count"] == 0


def test_rejects_readback_digest_generated_count(tmp_path):
    data = load_latest_ai_agent_owner_approved_result_capture_readback()
    bad = copy.deepcopy(data)
    bad["readback_truth"]["readback_digest_generated_count"] = 1
    bad["rollups"]["readback_digest_generated_count"] = 1
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="live counters"):
        load_latest_ai_agent_owner_approved_result_capture_readback(tmp_path)


def test_rejects_canonical_runtime_read_enabled(tmp_path):
    data = load_latest_ai_agent_owner_approved_result_capture_readback()
    bad = copy.deepcopy(data)
    bad["readback_truth"]["canonical_runtime_readback_enabled"] = True
    _write_snapshot(tmp_path, bad)

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


def test_rejects_digest_write_enabled(tmp_path):
    data = load_latest_ai_agent_owner_approved_result_capture_readback()
    bad = copy.deepcopy(data)
    bad["result_capture_readback_digests"][0]["write_enabled"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="write_enabled"):
        load_latest_ai_agent_owner_approved_result_capture_readback(tmp_path)


def test_rejects_promotion_allowed(tmp_path):
    data = load_latest_ai_agent_owner_approved_result_capture_readback()
    bad = copy.deepcopy(data)
    bad["promotion_readiness_reviews"][0]["promotion_allowed"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="promotion_allowed"):
        load_latest_ai_agent_owner_approved_result_capture_readback(tmp_path)


def test_rejects_reviewer_queue_write(tmp_path):
    data = load_latest_ai_agent_owner_approved_result_capture_readback()
    bad = copy.deepcopy(data)
    bad["reviewer_queue_preview"][0]["queue_write_enabled"] = True
    _write_snapshot(tmp_path, bad)

    with pytest.raises(ValueError, match="queue_write_enabled"):
        load_latest_ai_agent_owner_approved_result_capture_readback(tmp_path)


def test_rejects_forbidden_display_terms(tmp_path):
    data = load_latest_ai_agent_owner_approved_result_capture_readback()
    bad = copy.deepcopy(data)
    bad["operator_actions"][0]["operator_instruction"] = "不得顯示工作視窗內容"
    _write_snapshot(tmp_path, bad)

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


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

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