from __future__ import annotations

import json

import pytest

from src.services.offsite_escrow_readiness_status import (
    load_latest_offsite_escrow_readiness_status,
)


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

    loaded = load_latest_offsite_escrow_readiness_status(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_cards"] == 3


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

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


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

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


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

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


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

    with pytest.raises(ValueError, match="total_cards"):
        load_latest_offsite_escrow_readiness_status(tmp_path)


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

    with pytest.raises(ValueError, match="blocked_escrow_card_ids"):
        load_latest_offsite_escrow_readiness_status(tmp_path)


def test_offsite_escrow_readiness_status_rejects_credential_plaintext(tmp_path):
    snapshot = _snapshot()
    snapshot["readiness_cards"][1]["credential_exposure_status"] = "credential_plaintext"
    (tmp_path / "offsite_escrow_readiness_status_2026-06-05.json").write_text(
        json.dumps(snapshot),
        encoding="utf-8",
    )

    with pytest.raises(ValueError, match="credential exposure"):
        load_latest_offsite_escrow_readiness_status(tmp_path)


def test_offsite_escrow_readiness_status_requires_operator_denials(tmp_path):
    snapshot = _snapshot()
    snapshot["operator_contract"]["must_not_interpret_as"] = ["復原批准"]
    (tmp_path / "offsite_escrow_readiness_status_2026-06-05.json").write_text(
        json.dumps(snapshot),
        encoding="utf-8",
    )

    with pytest.raises(ValueError, match="must_not_interpret_as"):
        load_latest_offsite_escrow_readiness_status(tmp_path)


def test_offsite_escrow_readiness_status_fails_when_missing(tmp_path):
    with pytest.raises(FileNotFoundError):
        load_latest_offsite_escrow_readiness_status(tmp_path)


def _snapshot(
    *,
    generated_at: str = "2026-06-05T00:00:00+08:00",
    completion: int = 100,
) -> dict:
    return {
        "schema_version": "offsite_escrow_readiness_status_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-106",
            "next_task_id": "P1-305",
            "read_only_mode": True,
        },
        "rollups": {
            "total_cards": 3,
            "by_readiness": {"verified": 1, "action_required": 1, "blocked": 1},
            "by_kind": {
                "offsite_mirror": 1,
                "credential_escrow": 1,
                "k8s_resource_offsite": 1,
            },
            "verified_offsite_card_ids": ["offsite_rclone_full_sync"],
            "blocked_escrow_card_ids": ["credential_escrow_markers"],
            "action_required_card_ids": ["velero_k8s_resources"],
            "execution_blocked_card_ids": [
                "offsite_rclone_full_sync",
                "credential_escrow_markers",
                "velero_k8s_resources",
            ],
        },
        "readiness_cards": [
            _card("offsite_rclone_full_sync", "offsite_mirror", "verified"),
            _card("credential_escrow_markers", "credential_escrow", "blocked"),
            _card("velero_k8s_resources", "k8s_resource_offsite", "action_required"),
        ],
        "operator_contract": {
            "display_mode": "read_only_status",
            "success_notification_policy": "成功狀態不得即時通知洗版。",
            "failure_notification_policy": "失敗或阻擋維持 action-required。",
            "credential_display_policy": "只顯示 redacted metadata。",
            "must_not_interpret_as": [
                "復原批准",
                "異地同步批准",
                "credential marker 寫入批准",
                "完整 DR 綠燈",
            ],
        },
        "operation_boundaries": {
            "read_only_status_allowed": True,
            "backup_execution_allowed": False,
            "restore_execution_allowed": False,
            "offsite_sync_execution_allowed": False,
            "credential_marker_write_allowed": False,
            "credential_read_allowed": False,
            "secret_plaintext_allowed": False,
            "schedule_change_allowed": False,
            "workflow_write_allowed": False,
            "telegram_test_notification_allowed": False,
            "destructive_prune_allowed": False,
            "production_routing_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 _card(card_id: str, kind: str, readiness: str) -> dict:
    return {
        "card_id": card_id,
        "target_id": card_id,
        "display_name": card_id,
        "kind": kind,
        "readiness": readiness,
        "offsite_status": "verified" if readiness == "verified" else "not_applicable",
        "escrow_status": "missing_markers" if kind == "credential_escrow" else "not_applicable",
        "restore_drill_status": "approval_required",
        "credential_exposure_status": "redacted_only",
        "automation_gate_status": "read_only_allowed",
        "operator_summary": "只讀狀態。",
        "next_action": "維持只讀。",
        "evidence_refs": ["docs/evaluations/backup_dr_readiness_matrix_2026-06-04.json"],
        "blocked_operations": ["offsite_sync_execution", "credential_marker_write"],
    }
