from __future__ import annotations

from typing import Optional

from analogai.foundation.infrastructure.services.embedding_service import EmbeddingService  # type: ignore[import-not-found]
from analogai.deepthink.infrastructure import config as infra_config
from analogai.deepthink.messaging.fact.repositories.fact_repository import FactRepositoryPort
from analogai.deepthink.messaging.fact.services.fact_service import (
    FactServicePort,
    ConversationBackedFactService,
    InMemoryFactService,
    RepositoryBackedFactService,
)
from analogai.deepthink.messaging.fact.enums.fact_embedding_mode import FactEmbeddingMode


class FactServiceFactory:
    def __init__(
        self,
        conversation_service: Optional[object] = None,
        embedding_service: Optional[EmbeddingService] = None,
        repository: Optional[FactRepositoryPort] = None,
    ):
        self._conversation_service = conversation_service
        self._embedding_service = embedding_service
        self._repository = repository

    def create(self) -> FactServicePort:
        embedding_mode = (
            FactEmbeddingMode.ENABLED
            if infra_config.USE_VECTOR_SEARCH
            else FactEmbeddingMode.DISABLED
        )
        if self._repository is not None:
            return RepositoryBackedFactService(
                self._repository,
                embedding_service=self._embedding_service,
                embedding_mode=embedding_mode,
            )
        if self._conversation_service is None:
            return InMemoryFactService(
                embedding_service=self._embedding_service,
                embedding_mode=embedding_mode,
            )
        return ConversationBackedFactService(
            self._conversation_service,
            embedding_service=self._embedding_service,
            embedding_mode=embedding_mode,
        )
