"""
Ollama embedding service for generating text embeddings.
Uses nomic-embed-text model via Ollama API.
"""

import logging

import httpx

from app.core.config import get_settings

logger = logging.getLogger(__name__)


async def get_embedding(text: str) -> list[float]:
    """
    Generate embedding vector for text using Ollama nomic-embed-text model.
    """
    settings = get_settings()

    async with httpx.AsyncClient(timeout=60.0) as client:
        try:
            response = await client.post(
                f"{settings.ollama_base_url}/api/embeddings",
                json={
                    "model": settings.ollama_embedding_model,
                    "prompt": text,
                }
            )
            response.raise_for_status()
            data = response.json()
            embedding = data.get("embedding", [])

            if not embedding:
                logger.warning(f"Empty embedding returned for text length={len(text)}")
                return [0.0] * settings.qdrant_vector_size

            logger.debug(f"Generated embedding: dim={len(embedding)}")
            return embedding

        except httpx.HTTPStatusError as e:
            logger.error(f"Ollama API error: {e.response.status_code} - {e.response.text}")
            raise
        except httpx.RequestError as e:
            logger.error(f"Ollama connection error: {e}")
            raise
        except Exception as e:
            logger.error(f"Embedding generation failed: {e}")
            raise


async def get_embeddings_batch(texts: list[str]) -> list[list[float]]:
    """
    Generate embedding vectors for multiple texts.
    """
    embeddings = []
    for text in texts:
        embedding = await get_embedding(text)
        embeddings.append(embedding)
    return embeddings
