#
#   Imandra Inc.
#
#   speclogician/presentation/preview/registry.py
#   

from __future__ import annotations

from dataclasses import dataclass
from typing import Callable, Any

from speclogician.presentation.ctx import RenderCtx


@dataclass(frozen=True)
class PreviewSpec:
    """
    A preview entry in the registry.
    - make(ctx, variant) returns a *PM object* (pydantic model)
    - render(pm, ctx) returns a Rich renderable
    """
    name: str
    variants: tuple[str, ...]
    make: Callable[[RenderCtx, str], Any]
    render: Callable[[Any, RenderCtx], Any]


_REGISTRY: dict[str, PreviewSpec] = {}


def register(spec: PreviewSpec) -> None:
    _REGISTRY[spec.name] = spec


def get(name: str) -> PreviewSpec:
    if name not in _REGISTRY:
        raise KeyError(f"Unknown preview: {name}. Available: {', '.join(sorted(_REGISTRY))}")
    return _REGISTRY[name]


def list_previews() -> list[PreviewSpec]:
    return [*_REGISTRY.values()]