"""Local embedding using sentence-transformers."""

import logging
from sentence_transformers import SentenceTransformer

from ars25.core.config import get_settings

logger = logging.getLogger(__name__)

_model: SentenceTransformer | None = None


def get_model() -> SentenceTransformer:
    global _model
    if _model is None:
        settings = get_settings()
        logger.info("Loading embedding model: %s", settings.embedding_model)
        _model = SentenceTransformer(settings.embedding_model)
    return _model


def embed_texts(texts: list[str], batch_size: int = 64) -> list[list[float]]:
    """Embed a list of texts and return vectors."""
    model = get_model()
    embeddings = model.encode(texts, batch_size=batch_size, show_progress_bar=False)
    return [vec.tolist() for vec in embeddings]


def embed_query(text: str) -> list[float]:
    """Embed a single query string."""
    model = get_model()
    vec = model.encode(text)
    return vec.tolist()
