from abc import ABC, abstractmethod
from typing import Tuple, Optional, Dict
from integrations.services.ai_services.glm_service import GLMIntegrationService
from integrations.services.ai_services.openrouter_service import OpenRouterIntegrationService
from integrations.models.ai_services.glm import GLMIntegration
from integrations.models.ai_services.openrouter import OpenRouterIntegration
import logging

logger = logging.getLogger(__name__)


class BaseAIProductService(ABC):
    """
    Абстрактный базовый класс для AI-сервисов продуктов
    """

    @abstractmethod
    def generate(self, **kwargs) -> Tuple[bool, str, Optional[Dict]]:
        """
        Основной метод генерации
        """
        pass

    @classmethod
    def get_glm_service(cls) -> Optional[GLMIntegrationService]:
        """
        Получить сервис GLM из активной интеграции
        """
        try:
            integration = GLMIntegration.objects.filter(is_active=True).first()
            if integration:
                return GLMIntegrationService(integration)
        except Exception as e:
            logger.error(f"Ошибка при получении GLM сервиса: {str(e)}")
        return None

    @classmethod
    def get_openrouter_service(cls) -> Optional[OpenRouterIntegrationService]:
        """
        Получить сервис OpenRouter из активной интеграции
        """
        try:
            integration = OpenRouterIntegration.objects.filter(is_active=True).first()
            if integration:
                return OpenRouterIntegrationService(integration)
        except Exception as e:
            logger.error(f"Ошибка при получении OpenRouter сервиса: {str(e)}")
        return None
