"""Backend registry and factory."""

from .base import BaseBackend, SearchHit, SearchResult

BACKEND_REGISTRY: dict[str, type[BaseBackend]] = {}


def register_backend(name: str):
    """Decorator to register a backend implementation."""

    def decorator(cls: type[BaseBackend]) -> type[BaseBackend]:
        BACKEND_REGISTRY[name] = cls
        return cls

    return decorator


def get_backend(name: str, **config) -> BaseBackend:
    """Get a backend instance by name."""
    if name not in BACKEND_REGISTRY:
        available = ', '.join(BACKEND_REGISTRY.keys())
        raise ValueError(f'Unknown backend: {name}. Available: {available}')
    return BACKEND_REGISTRY[name](**config)


# Import backends to trigger registration
from . import meilisearch  # noqa: F401, E402

# Conditionally import seekdb if pyseekdb is installed
try:
    from . import seekdb  # noqa: F401
except ImportError:
    pass  # pyseekdb not installed, seekdb backend unavailable

__all__ = [
    'BaseBackend',
    'SearchHit',
    'SearchResult',
    'BACKEND_REGISTRY',
    'register_backend',
    'get_backend',
]
