"""Top-level embedding facade."""

from __future__ import annotations

from typing import Any

from thryve.config import EmbeddingConfig
from thryve.core.backends.adapter import BackendAdapter


class EmbeddingModel:
    """Thin wrapper that provides an ``embed()`` callable for the memory system."""

    def __init__(self, adapter: BackendAdapter, model: str | None = None) -> None:
        self._adapter = adapter
        self._model = model

    def embed(self, texts: list[str]) -> list[list[float]]:
        kwargs: dict[str, Any] = {}
        if self._model:
            kwargs["model"] = self._model
        return self._adapter.embeddings(texts, **kwargs)

    def __call__(self, texts: list[str]) -> list[list[float]]:
        return self.embed(texts)


def create_embedding(config: EmbeddingConfig | None = None) -> EmbeddingModel:
    """Create an :class:`EmbeddingModel` from config."""
    if config is None:
        config = EmbeddingConfig()

    import thryve.core.backends  # noqa: F401 – ensure backends are registered

    params: dict[str, Any] = {"model": config.model}
    if config.api_key:
        params["api_key"] = config.api_key
    if config.base_url:
        params["base_url"] = config.base_url

    adapter = BackendAdapter(config.backend, params)
    return EmbeddingModel(adapter, model=config.model)
