"""
AI Provider Factory (Factory Pattern)
"""
import logging
from typing import Dict, Type

from core import AIProcessingError
from .base_provider import AIProvider
from .claude_provider import ClaudeProvider
from .gemini_provider import GeminiProvider


class AIProviderFactory:
    """Factory for creating AI providers with fallback"""

    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self._providers: Dict[str, AIProvider] = {
            'claude': ClaudeProvider(),
            'gemini': GeminiProvider()
        }

    def get_provider(self, preferred: str = 'gemini') -> AIProvider:
        """Get available provider with fallback"""
        # Try preferred provider first
        if preferred in self._providers:
            provider = self._providers[preferred]
            if provider.is_available():
                self.logger.info(f"Using {preferred} provider")
                return provider

        # Fallback to any available provider
        for name, provider in self._providers.items():
            if provider.is_available():
                self.logger.info(f"Falling back to {name} provider")
                return provider

        raise AIProcessingError("No AI providers available")

    def get_all_available(self) -> Dict[str, AIProvider]:
        """Get all available providers"""
        return {
            name: provider
            for name, provider in self._providers.items()
            if provider.is_available()
        }

    def get_best_provider(self) -> AIProvider:
        """Get the best available provider (Gemini > Claude)"""
        return self.get_provider('gemini')


# Global instance
ai_provider_factory = AIProviderFactory()
