"""
Factory for creating LLM service instances.
"""
import logging
from typing import Optional, Dict, Any
from django.conf import settings
from .llm_service import LLMService
from .groq import GroqService
from .openai import OpenAIService

logger = logging.getLogger(__name__)

class LLMServiceFactory:
    """
    Factory class for creating LLM service instances.
    """
    
    @staticmethod
    def get_service(provider: Optional[str] = None) -> LLMService:
        """
        Get an LLM service instance based on provider.
        
        Args:
            provider: LLM provider name (e.g., 'groq', 'openai')
                     If None, uses settings.DEFAULT_LLM_PROVIDER
                     
        Returns:
            An instance of the appropriate LLM service
            
        Raises:
            ValueError: If the provider is not supported
        """
        if provider is None:
            provider = getattr(settings, 'DEFAULT_LLM_PROVIDER', 'groq')
            
        provider = provider.lower()
        
        if provider == 'groq':
            return GroqService()
        elif provider == 'openai':
            return OpenAIService()
        else:
            logger.error(f"Unsupported LLM provider: {provider}")
            raise ValueError(f"Unsupported LLM provider: {provider}")
    
    @staticmethod
    def get_available_providers() -> Dict[str, Dict[str, str]]:
        """
        Get a dictionary of available providers and their models.
        
        Returns:
            Dictionary with provider names as keys and their models as values
        """
        providers = {}
        
        # Add Groq models if configured
        if hasattr(settings, 'GROQ_MODELS'):
            providers['groq'] = getattr(settings, 'GROQ_MODELS', {})
            
        # Add OpenAI models if configured
        if hasattr(settings, 'OPENAI_MODELS'):
            providers['openai'] = getattr(settings, 'OPENAI_MODELS', {})
            
        return providers 