"""
AI Agent report runtime readiness snapshot.

Loads the latest committed P2-403L report delivery, Telegram receipt, AI
analysis, and medium / low risk automation readiness gate. This module does
not schedule reports, write Telegram Gateway queues, start AI workers, or
optimize production.
"""

from __future__ import annotations

import json
from pathlib import Path
from typing import Any

from src.services.snapshot_paths import default_evaluations_dir

_DEFAULT_EVALUATIONS_DIR = default_evaluations_dir(Path(__file__))
_SNAPSHOT_PATTERN = "ai_agent_report_runtime_readiness_*.json"
_SCHEMA_VERSION = "ai_agent_report_runtime_readiness_v1"
_RUNTIME_AUTHORITY = "report_runtime_readiness_only_no_live_delivery_or_optimization"


def load_latest_ai_agent_report_runtime_readiness(
    evaluations_dir: Path | None = None,
) -> dict[str, Any]:
    """Load the newest committed AI Agent report runtime readiness snapshot."""
    directory = evaluations_dir or _DEFAULT_EVALUATIONS_DIR
    candidates = sorted(directory.glob(_SNAPSHOT_PATTERN))
    if not candidates:
        raise FileNotFoundError(f"no AI Agent report runtime readiness snapshots found in {directory}")

    latest = candidates[-1]
    with latest.open(encoding="utf-8") as handle:
        payload = json.load(handle)

    if not isinstance(payload, dict):
        raise ValueError(f"{latest}: expected JSON object")
    _require_schema(payload, str(latest))
    _require_activation_boundaries(payload, str(latest))
    _require_lane_contract(payload, str(latest))
    _require_policy_contract(payload, str(latest))
    _require_telegram_contract(payload, str(latest))
    _require_rollup_consistency(payload, str(latest))
    return payload


def _require_schema(payload: dict[str, Any], label: str) -> None:
    if payload.get("schema_version") != _SCHEMA_VERSION:
        raise ValueError(f"{label}: expected schema_version={_SCHEMA_VERSION}")
    status = payload.get("program_status") or {}
    if status.get("read_only_mode") is not True:
        raise ValueError(f"{label}: program_status.read_only_mode must be true")
    if status.get("runtime_authority") != _RUNTIME_AUTHORITY:
        raise ValueError(f"{label}: runtime_authority must remain {_RUNTIME_AUTHORITY}")
    if status.get("current_task_id") != "P2-403L":
        raise ValueError(f"{label}: current_task_id must be P2-403L")


def _require_activation_boundaries(payload: dict[str, Any], label: str) -> None:
    truth = payload.get("activation_truth") or {}
    ready_flags = {
        "report_scheduler_contract_ready",
        "telegram_gateway_queue_contract_ready",
        "telegram_delivery_receipt_contract_ready",
        "ai_readback_analysis_contract_ready",
        "medium_low_auto_guard_contract_ready",
        "high_risk_approval_gate_contract_ready",
    }
    missing_ready = sorted(flag for flag in ready_flags if truth.get(flag) is not True)
    if missing_ready:
        raise ValueError(f"{label}: readiness contract flags must remain true: {missing_ready}")

    false_flags = {
        "live_report_delivery_enabled",
        "telegram_gateway_queue_write_enabled",
        "report_read_receipt_write_enabled",
        "ai_analysis_runtime_enabled",
        "medium_low_auto_worker_enabled",
        "production_optimization_enabled",
        "high_risk_auto_execution_enabled",
    }
    unsafe_flags = sorted(flag for flag in false_flags if truth.get(flag) is not False)
    if unsafe_flags:
        raise ValueError(f"{label}: live runtime flags must remain false: {unsafe_flags}")

    zero_counts = {
        "live_report_delivery_count_24h",
        "telegram_gateway_queue_write_count_24h",
        "report_read_receipt_count_24h",
        "ai_analysis_runtime_count_24h",
        "medium_low_auto_execution_count_24h",
        "production_optimization_count_24h",
        "high_risk_auto_execution_count_24h",
    }
    non_zero = sorted(key for key in zero_counts if truth.get(key) != 0)
    if non_zero:
        raise ValueError(f"{label}: live report runtime counts must remain zero: {non_zero}")


def _require_lane_contract(payload: dict[str, Any], label: str) -> None:
    lanes = payload.get("runtime_lanes") or []
    lane_ids = {lane.get("lane_id") for lane in lanes}
    required_lanes = {
        "report_scheduler",
        "telegram_gateway_queue",
        "telegram_delivery_receipt",
        "ai_post_report_analysis",
        "medium_low_auto_guard",
        "high_risk_approval",
        "post_action_verifier",
    }
    if lane_ids != required_lanes:
        raise ValueError(f"{label}: runtime lanes must match {sorted(required_lanes)}")

    agents = {lane.get("owner_agent") for lane in lanes}
    if not {"openclaw", "hermes", "nemotron"}.issubset(agents):
        raise ValueError(f"{label}: runtime lanes must include OpenClaw, Hermes, and NemoTron ownership")

    live_lanes = sorted(lane.get("lane_id") for lane in lanes if lane.get("current_live_count_24h") != 0)
    if live_lanes:
        raise ValueError(f"{label}: lane live counts must remain zero: {live_lanes}")


def _require_policy_contract(payload: dict[str, Any], label: str) -> None:
    policies = payload.get("automation_policies") or []
    policy_ids = {policy.get("risk_id") for policy in policies}
    if policy_ids != {"low", "medium", "high", "critical"}:
        raise ValueError(f"{label}: automation policies must include low, medium, high, critical")

    for policy in policies:
        risk_id = policy.get("risk_id")
        if policy.get("current_execution_enabled") is not False:
            raise ValueError(f"{label}: policy {risk_id} current_execution_enabled must remain false")
        if risk_id in {"high", "critical"}:
            if policy.get("approval_required") is not True:
                raise ValueError(f"{label}: policy {risk_id} must require approval")
            if policy.get("auto_allowed_after_guard") is not False:
                raise ValueError(f"{label}: policy {risk_id} cannot be auto allowed")
        if risk_id in {"low", "medium"} and policy.get("auto_allowed_after_guard") is not True:
            raise ValueError(f"{label}: policy {risk_id} must be auto allowed only after guard")


def _require_telegram_contract(payload: dict[str, Any], label: str) -> None:
    route = payload.get("telegram_route_readiness") or {}
    if route.get("canonical_room") != "AwoooI SRE 戰情室":
        raise ValueError(f"{label}: canonical Telegram room must remain AwoooI SRE 戰情室")
    if route.get("gateway_required") is not True:
        raise ValueError(f"{label}: Telegram Gateway must be required")

    false_fields = {
        "direct_bot_api_allowed",
        "bot_log_out_allowed",
        "telegram_gateway_queue_write_enabled",
        "e2e_delivery_verified",
        "delivery_receipt_write_enabled",
    }
    unsafe = sorted(field for field in false_fields if route.get(field) is not False)
    if unsafe:
        raise ValueError(f"{label}: Telegram live route fields must remain false: {unsafe}")


def _require_rollup_consistency(payload: dict[str, Any], label: str) -> None:
    rollups = payload.get("rollups") or {}
    lanes = payload.get("runtime_lanes") or []
    cadences = payload.get("report_delivery_cadence_gates") or []
    decisions = payload.get("operator_decisions") or []
    policies = payload.get("automation_policies") or []
    truth = payload.get("activation_truth") or {}

    expected = {
        "runtime_lane_count": len(lanes),
        "report_cadence_gate_count": len(cadences),
        "operator_decision_count": len(decisions),
        "automation_policy_count": len(policies),
        "ready_contract_count": len([lane for lane in lanes if lane.get("contract_status") == "ready_for_owner_review"]),
        "blocked_contract_count": len([lane for lane in lanes if lane.get("contract_status") == "blocked_by_runtime_gate"]),
        "current_enabled_count": 0,
        "live_report_delivery_count": truth.get("live_report_delivery_count_24h"),
        "live_ai_analysis_count": truth.get("ai_analysis_runtime_count_24h"),
        "live_medium_low_auto_execution_count": truth.get("medium_low_auto_execution_count_24h"),
        "telegram_gateway_queue_write_count": truth.get("telegram_gateway_queue_write_count_24h"),
        "high_risk_auto_execution_count": truth.get("high_risk_auto_execution_count_24h"),
    }
    mismatched = {
        key: {"expected": value, "actual": rollups.get(key)}
        for key, value in expected.items()
        if rollups.get(key) != value
    }
    if mismatched:
        raise ValueError(f"{label}: rollup counts must match payload sections: {mismatched}")

    approval_required = sorted(
        decision.get("decision_id")
        for decision in decisions
        if decision.get("approval_required") is True
    )
    if sorted(rollups.get("approval_required_decision_ids") or []) != approval_required:
        raise ValueError(f"{label}: approval_required_decision_ids mismatch")
