"""Policy preset loading — pure functions, no gRPC dependency."""

from __future__ import annotations

import pathlib
from typing import Any

import yaml

_PRESETS_DIR = pathlib.Path(__file__).parent / "presets"


def list_presets() -> list[dict[str, str]]:
    """List available policy presets bundled with Shoreguard."""
    presets: list[dict[str, str]] = []
    if not _PRESETS_DIR.exists():
        return presets
    for path in sorted(_PRESETS_DIR.glob("*.yaml")):
        data = yaml.safe_load(path.read_text())
        preset_meta = data.get("preset", {})
        presets.append(
            {
                "name": preset_meta.get("name", path.stem),
                "description": preset_meta.get("description", ""),
                "file": path.name,
            }
        )
    return presets


def get_preset(preset_name: str) -> dict[str, Any] | None:
    """Load a single preset by name. Returns None if not found."""
    preset_path = _PRESETS_DIR / f"{preset_name}.yaml"
    if not preset_path.exists():
        return None
    return yaml.safe_load(preset_path.read_text())
