import copy

import pytest

from src.services.ai_agent_owner_approved_fixture_dry_run import (
    load_latest_ai_agent_owner_approved_fixture_dry_run,
)


def test_load_latest_ai_agent_owner_approved_fixture_dry_run():
    data = load_latest_ai_agent_owner_approved_fixture_dry_run()

    assert data["schema_version"] == "ai_agent_owner_approved_fixture_dry_run_v1"
    assert data["program_status"]["current_task_id"] == "P2-403F"
    assert data["program_status"]["next_task_id"] == "P2-403G"
    assert data["program_status"]["overall_completion_percent"] == 88
    assert data["dry_run_truth"]["owner_fixture_scope_approved"] is True
    assert data["dry_run_truth"]["fixture_dry_run_allowed"] is True
    assert data["dry_run_truth"]["production_write_approved"] is False
    assert data["dry_run_truth"]["telegram_send_allowed"] is False
    assert data["rollups"]["fixture_set_count"] == len(data["fixture_sets"])
    assert data["rollups"]["live_write_count_total"] == 0
    assert data["rollups"]["live_send_count_total"] == 0


def test_rejects_production_write_approval(tmp_path):
    data = load_latest_ai_agent_owner_approved_fixture_dry_run()
    bad = copy.deepcopy(data)
    bad["dry_run_truth"]["production_write_approved"] = True
    path = tmp_path / "ai_agent_owner_approved_fixture_dry_run_2026-06-11.json"
    path.write_text(__import__("json").dumps(bad), encoding="utf-8")

    with pytest.raises(ValueError, match="dry-run truth flags"):
        load_latest_ai_agent_owner_approved_fixture_dry_run(tmp_path)


def test_rejects_action_button_display(tmp_path):
    data = load_latest_ai_agent_owner_approved_fixture_dry_run()
    bad = copy.deepcopy(data)
    bad["display_redaction_contract"]["action_button_allowed"] = True
    path = tmp_path / "ai_agent_owner_approved_fixture_dry_run_2026-06-11.json"
    path.write_text(__import__("json").dumps(bad), encoding="utf-8")

    with pytest.raises(ValueError, match="action_button_allowed"):
        load_latest_ai_agent_owner_approved_fixture_dry_run(tmp_path)


def test_rejects_rollup_mismatch(tmp_path):
    data = load_latest_ai_agent_owner_approved_fixture_dry_run()
    bad = copy.deepcopy(data)
    bad["rollups"]["fixture_set_count"] = 999
    path = tmp_path / "ai_agent_owner_approved_fixture_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_fixture_dry_run(tmp_path)
