from __future__ import annotations

from functools import lru_cache

from api.clients.chroma_store import ChromaVectorStore
from api.clients.openrouter_client import build_openai_client
from api.config import settings
from api.services.indexing import IndexingService
from api.services.legal_ai import LegalAIService
from api.services.local_embeddings import get_embedding_service
from api.services.retrieval import HybridRetrievalService
from shared import ORM


@lru_cache(maxsize=1)
def get_orm() -> ORM:
    return ORM()


@lru_cache(maxsize=1)
def get_vector_store() -> ChromaVectorStore:
    return ChromaVectorStore()


@lru_cache(maxsize=1)
def get_ai_service() -> LegalAIService:
    return LegalAIService(build_openai_client(), settings.llm_model)


@lru_cache(maxsize=1)
def get_retrieval_service() -> HybridRetrievalService:
    return HybridRetrievalService(
        orm=get_orm(),
        embedder=get_embedding_service(),
        vector_store=get_vector_store(),
    )


@lru_cache(maxsize=1)
def get_indexing_service() -> IndexingService:
    return IndexingService(
        orm=get_orm(),
        embedder=get_embedding_service(),
        vector_store=get_vector_store(),
    )
