import copy

import pytest

from src.services.ai_agent_owner_approved_learning_dry_run import (
    load_latest_ai_agent_owner_approved_learning_dry_run,
)


def test_load_latest_ai_agent_owner_approved_learning_dry_run():
    data = load_latest_ai_agent_owner_approved_learning_dry_run()

    assert data["schema_version"] == "ai_agent_owner_approved_learning_dry_run_v1"
    assert data["program_status"]["current_task_id"] == "P2-403F"
    assert data["program_status"]["next_task_id"] == "P2-403G"
    assert data["dry_run_truth"]["owner_approval_received_count"] == 0
    assert data["dry_run_truth"]["dry_run_preview_allowed"] is True
    assert data["dry_run_truth"]["dry_run_preview_generated_count"] == 0
    assert data["dry_run_truth"]["km_write_allowed"] is False
    assert data["dry_run_truth"]["playbook_trust_write_allowed"] is False
    assert data["rollups"]["operator_action_count"] == len(data["operator_actions"])


def test_rejects_learning_write_enabled(tmp_path):
    data = load_latest_ai_agent_owner_approved_learning_dry_run()
    bad = copy.deepcopy(data)
    bad["dry_run_truth"]["km_write_allowed"] = True
    path = tmp_path / "ai_agent_owner_approved_learning_dry_run_2026-06-11.json"
    path.write_text(__import__("json").dumps(bad), encoding="utf-8")

    with pytest.raises(ValueError, match="learning write flags"):
        load_latest_ai_agent_owner_approved_learning_dry_run(tmp_path)


def test_rejects_owner_approval_count_increment(tmp_path):
    data = load_latest_ai_agent_owner_approved_learning_dry_run()
    bad = copy.deepcopy(data)
    bad["dry_run_truth"]["owner_approval_received_count"] = 1
    path = tmp_path / "ai_agent_owner_approved_learning_dry_run_2026-06-11.json"
    path.write_text(__import__("json").dumps(bad), encoding="utf-8")

    with pytest.raises(ValueError, match="counts must remain zero"):
        load_latest_ai_agent_owner_approved_learning_dry_run(tmp_path)


def test_rejects_missing_operator_action_type(tmp_path):
    data = load_latest_ai_agent_owner_approved_learning_dry_run()
    bad = copy.deepcopy(data)
    bad["operator_actions"] = [
        action for action in bad["operator_actions"] if action["action_type"] != "reject_or_rework"
    ]
    bad["rollups"]["operator_action_count"] = len(bad["operator_actions"])
    path = tmp_path / "ai_agent_owner_approved_learning_dry_run_2026-06-11.json"
    path.write_text(__import__("json").dumps(bad), encoding="utf-8")

    with pytest.raises(ValueError, match="operator actions"):
        load_latest_ai_agent_owner_approved_learning_dry_run(tmp_path)


def test_rejects_rollup_mismatch(tmp_path):
    data = load_latest_ai_agent_owner_approved_learning_dry_run()
    bad = copy.deepcopy(data)
    bad["rollups"]["dry_run_gate_count"] = 999
    path = tmp_path / "ai_agent_owner_approved_learning_dry_run_2026-06-11.json"
    path.write_text(__import__("json").dumps(bad), encoding="utf-8")

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