from typing import Optional, TYPE_CHECKING
import importlib

from analogai.foundation.common.logging.app_logger import app_logger

if TYPE_CHECKING:
    from analogai.deepthink.startup.factories.domain_service_factory import DomainServiceFactory


class ExtensionsServiceFactory:
    def __init__(
        self,
        domain_service_factory: "DomainServiceFactory",
        env=None,
        config: Optional[object] = None,
    ):
        self._domain_service_factory = domain_service_factory
        self._env = env
        self._config = config
        self._conversation_service = None
        self._embedding_service = None
        self._authorization_factory = None


    def get_conversation_service(self):
        if self._conversation_service is None:
            from analogai.foundation.extensions.conversation.factories.conversation_service_factory import (
                ConversationServiceFactory,
            )

            self._conversation_service = ConversationServiceFactory().create()
        return self._conversation_service


    def get_embedding_service(self):
        if self._embedding_service is not None:
            return self._embedding_service
        for module_path in _default_embedding_module_paths():
            try:
                module = importlib.import_module(module_path)
            except Exception:
                continue
            try:
                factory_cls = module.EmbeddingServiceFactory
            except AttributeError:
                continue
            try:
                factory = factory_cls()
            except Exception:
                continue
            try:
                self._embedding_service = factory.create()
            except AttributeError:
                self._embedding_service = None
            except Exception:
                self._embedding_service = None
            if self._embedding_service is None:
                try:
                    self._embedding_service = factory.get_service()
                except AttributeError:
                    self._embedding_service = None
                except Exception:
                    self._embedding_service = None
            if self._embedding_service is not None:
                return self._embedding_service
        try:
            app_logger.debug("[ExtensionsServiceFactory] embedding service unavailable")
        except Exception:
            pass
        return None


    def get_authorization_factory(self):
        if self._authorization_factory is None:
            from analogai.foundation.extensions.authorization.authorization_factory import AuthorizationFactory

            self._authorization_factory = AuthorizationFactory()
        return self._authorization_factory


    def get_statement_derivation_service(self):
        return _create_statement_derivation_service(self._domain_service_factory)



def _create_statement_derivation_service(domain_service_factory: "DomainServiceFactory"):
    return domain_service_factory.get_direct_statement_service()


def _default_embedding_module_paths() -> tuple[str, ...]:
    return (
        "analogai.foundation.extensions.embedding.factories.embedding_service_factory",
        "analogai.foundation.extensions.embedding.embedding_service_factory",
        "analogai.foundation.extensions.embedding.factory",
    )
