from __future__ import annotations

import json

import pytest

from src.services.backup_restore_drill_approval_package_template import (
    load_latest_backup_restore_drill_approval_package_template,
)


def test_load_latest_backup_restore_drill_approval_package_template_reads_newest_file(tmp_path):
    older = _snapshot(generated_at="2026-06-04T00:00:00+08:00", completion=75)
    newer = _snapshot(generated_at="2026-06-05T00:00:00+08:00", completion=100)
    (tmp_path / "backup_restore_drill_approval_package_template_2026-06-04.json").write_text(
        json.dumps(older),
        encoding="utf-8",
    )
    (tmp_path / "backup_restore_drill_approval_package_template_2026-06-05.json").write_text(
        json.dumps(newer),
        encoding="utf-8",
    )

    loaded = load_latest_backup_restore_drill_approval_package_template(tmp_path)

    assert loaded["generated_at"] == "2026-06-05T00:00:00+08:00"
    assert loaded["program_status"]["overall_completion_percent"] == 100
    assert loaded["rollups"]["total_templates"] == 2
    assert loaded["operation_boundaries"]["restore_execution_allowed"] is False


def test_backup_restore_drill_approval_package_template_requires_read_only_mode(tmp_path):
    snapshot = _snapshot()
    snapshot["program_status"]["read_only_mode"] = False
    (tmp_path / "backup_restore_drill_approval_package_template_2026-06-05.json").write_text(
        json.dumps(snapshot),
        encoding="utf-8",
    )

    with pytest.raises(ValueError, match="read_only_mode"):
        load_latest_backup_restore_drill_approval_package_template(tmp_path)


def test_backup_restore_drill_approval_package_template_requires_blocked_operations(tmp_path):
    snapshot = _snapshot()
    snapshot["operation_boundaries"]["restore_execution_allowed"] = True
    (tmp_path / "backup_restore_drill_approval_package_template_2026-06-05.json").write_text(
        json.dumps(snapshot),
        encoding="utf-8",
    )

    with pytest.raises(ValueError, match="operation boundaries"):
        load_latest_backup_restore_drill_approval_package_template(tmp_path)


def test_backup_restore_drill_approval_package_template_requires_blocked_approval_boundaries(tmp_path):
    snapshot = _snapshot()
    snapshot["approval_boundaries"]["credential_marker_write_allowed"] = True
    (tmp_path / "backup_restore_drill_approval_package_template_2026-06-05.json").write_text(
        json.dumps(snapshot),
        encoding="utf-8",
    )

    with pytest.raises(ValueError, match="approval boundaries"):
        load_latest_backup_restore_drill_approval_package_template(tmp_path)


def test_backup_restore_drill_approval_package_template_requires_total_consistency(tmp_path):
    snapshot = _snapshot()
    snapshot["rollups"]["total_templates"] = 999
    (tmp_path / "backup_restore_drill_approval_package_template_2026-06-05.json").write_text(
        json.dumps(snapshot),
        encoding="utf-8",
    )

    with pytest.raises(ValueError, match="total_templates"):
        load_latest_backup_restore_drill_approval_package_template(tmp_path)


def test_backup_restore_drill_approval_package_template_requires_ready_id_consistency(tmp_path):
    snapshot = _snapshot()
    snapshot["rollups"]["template_ready_ids"] = []
    (tmp_path / "backup_restore_drill_approval_package_template_2026-06-05.json").write_text(
        json.dumps(snapshot),
        encoding="utf-8",
    )

    with pytest.raises(ValueError, match="template_ready_ids"):
        load_latest_backup_restore_drill_approval_package_template(tmp_path)


def test_backup_restore_drill_approval_package_template_requires_hitl_consistency(tmp_path):
    snapshot = _snapshot()
    snapshot["rollups"]["hitl_required_template_ids"] = []
    (tmp_path / "backup_restore_drill_approval_package_template_2026-06-05.json").write_text(
        json.dumps(snapshot),
        encoding="utf-8",
    )

    with pytest.raises(ValueError, match="hitl_required_template_ids"):
        load_latest_backup_restore_drill_approval_package_template(tmp_path)


def test_backup_restore_drill_approval_package_template_requires_source_target_rollups(tmp_path):
    snapshot = _snapshot()
    snapshot["rollups"]["blocked_source_target_ids"] = []
    (tmp_path / "backup_restore_drill_approval_package_template_2026-06-05.json").write_text(
        json.dumps(snapshot),
        encoding="utf-8",
    )

    with pytest.raises(ValueError, match="blocked_source_target_ids"):
        load_latest_backup_restore_drill_approval_package_template(tmp_path)


def test_backup_restore_drill_approval_package_template_requires_hitl_gate(tmp_path):
    snapshot = _snapshot()
    snapshot["decision_gate_contract"]["hitl_required"] = False
    (tmp_path / "backup_restore_drill_approval_package_template_2026-06-05.json").write_text(
        json.dumps(snapshot),
        encoding="utf-8",
    )

    with pytest.raises(ValueError, match="hitl_required"):
        load_latest_backup_restore_drill_approval_package_template(tmp_path)


def test_backup_restore_drill_approval_package_template_fails_when_missing(tmp_path):
    with pytest.raises(FileNotFoundError):
        load_latest_backup_restore_drill_approval_package_template(tmp_path)


def _snapshot(
    *,
    generated_at: str = "2026-06-05T00:00:00+08:00",
    completion: int = 100,
) -> dict:
    return {
        "schema_version": "backup_restore_drill_approval_package_template_v1",
        "generated_at": generated_at,
        "source_refs": ["docs/evaluations/backup_dr_readiness_matrix_2026-06-04.json"],
        "program_status": {
            "overall_completion_percent": completion,
            "current_priority": "P1",
            "current_task_id": "P1-105",
            "next_task_id": "P1-106",
            "read_only_mode": True,
        },
        "rollups": {
            "total_templates": 2,
            "by_domain": {"database_restore": 1, "k8s_resource_restore": 1},
            "template_ready_ids": [
                "database_restore_drill_approval_package",
                "velero_k8s_restore_drill_package",
            ],
            "hitl_required_template_ids": [
                "database_restore_drill_approval_package",
                "velero_k8s_restore_drill_package",
            ],
            "blocked_source_target_ids": ["configs_capture"],
            "action_required_source_target_ids": ["velero_k8s_resources"],
        },
        "approval_fields": [
            {
                "field_id": "manual_approval",
                "required": True,
                "description": "approval",
            }
        ],
        "package_templates": [
            _template(
                "database_restore_drill_approval_package",
                "database_restore",
                [{"target_id": "configs_capture", "readiness": "blocked"}],
            ),
            _template(
                "velero_k8s_restore_drill_package",
                "k8s_resource_restore",
                [{"target_id": "velero_k8s_resources", "readiness": "action_required"}],
            ),
        ],
        "decision_gate_contract": {
            "openclaw_role": "arbitrate",
            "hermes_role": "summarize",
            "nemotron_role": "offline review",
            "hitl_required": True,
            "expires_after": "7 days",
            "invalidated_by": ["snapshot change"],
        },
        "operation_boundaries": {
            "read_only_template_allowed": True,
            "backup_execution_allowed": False,
            "restore_execution_allowed": False,
            "offsite_sync_execution_allowed": False,
            "credential_marker_write_allowed": False,
            "schedule_change_allowed": False,
            "workflow_write_allowed": False,
            "telegram_test_notification_allowed": False,
            "destructive_prune_allowed": False,
            "secret_plaintext_allowed": False,
            "production_routing_allowed": False,
            "sdk_installation_allowed": False,
            "paid_api_call_allowed": False,
            "shadow_or_canary_allowed": False,
        },
        "approval_boundaries": {
            "sdk_installation_allowed": False,
            "paid_api_call_allowed": False,
            "shadow_or_canary_allowed": False,
            "production_routing_allowed": False,
            "destructive_operation_allowed": False,
            "restore_execution_allowed": False,
            "offsite_sync_execution_allowed": False,
            "credential_marker_write_allowed": False,
        },
    }


def _template(template_id: str, domain: str, source_target_statuses: list[dict]) -> dict:
    return {
        "template_id": template_id,
        "domain": domain,
        "status": "template_ready",
        "owner_agent": "openclaw",
        "purpose": "approval package",
        "source_target_statuses": source_target_statuses,
        "required_evidence": ["docs/evaluations/backup_dr_readiness_matrix_2026-06-04.json"],
        "required_decisions": ["approve or reject"],
        "required_prechecks": ["precheck"],
        "required_tests": ["schema validation"],
        "rollback_requirements": ["rollback plan"],
        "abort_criteria": ["abort"],
        "manual_approvals": ["OpenClaw arbitration", "HITL approval"],
        "prohibited_without_approval": ["restore execution"],
        "evidence_refs": ["docs/evaluations/backup_dr_readiness_matrix_2026-06-04.json"],
    }
