"""Embedding provider abstraction with lazy loading."""

from __future__ import annotations

import logging
from typing import Any

logger = logging.getLogger(__name__)


class EmbeddingProvider:
    """Lazy-loaded sentence-transformers embedding provider."""

    def __init__(self, model_name: str = "all-MiniLM-L6-v2") -> None:
        self._model_name = model_name
        self._model: Any = None

    def _load_model(self) -> None:
        """Load the sentence-transformer model on first use."""
        if self._model is not None:
            return
        from sentence_transformers import SentenceTransformer

        logger.info("Loading embedding model: %s", self._model_name)
        self._model = SentenceTransformer(self._model_name, device="cpu")

    def embed(self, texts: list[str]) -> list[list[float]]:
        """Embed a list of texts.

        Returns:
            List of embedding vectors (one per input text).
        """
        self._load_model()
        embeddings = self._model.encode(texts, show_progress_bar=False)
        return [e.tolist() for e in embeddings]

    def embed_single(self, text: str) -> list[float]:
        """Embed a single text string."""
        return self.embed([text])[0]

    @property
    def dimension(self) -> int:
        """Return the embedding dimension."""
        self._load_model()
        return int(self._model.get_sentence_embedding_dimension())
