"""
服务提供者
提供服务注册和初始化的标准化接口
"""
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from .container import Container


class ServiceProvider(ABC):
    """
    服务提供者基类
    
    使用示例:
        class MyServiceProvider(ServiceProvider):
            def register(self, container: Container):
                container.register_singleton("my_service", lambda: MyService())
            
            def boot(self, container: Container):
                service = container.get("my_service")
                service.initialize()
    """
    
    @abstractmethod
    def register(self, container: "Container"):
        """
        注册服务到容器
        
        Args:
            container: 容器实例
        """
        pass
    
    def boot(self, container: "Container"):
        """
        启动服务（在所有服务注册后调用）
        
        Args:
            container: 容器实例
        """
        pass


class ConfigServiceProvider(ServiceProvider):
    """配置服务提供者"""
    
    def register(self, container: "Container"):
        """注册配置服务"""
        from src.config import get_config_manager
        
        # 注册配置管理器（单例）
        container.register_singleton("config", get_config_manager)
    
    def boot(self, container: "Container"):
        """初始化配置"""
        config = container.get("config")
        config.ensure_directories()


class RAGServiceProvider(ServiceProvider):
    """RAG服务提供者"""
    
    def register(self, container: "Container"):
        """注册RAG相关服务"""
        # 注册 RAGService（懒加载单例）
        def create_rag_service():
            from src.services import RAGService
            config = container.get("config")
            return RAGService(config=config)
        
        container.register_singleton("rag_service", create_rag_service)
        
        # 注册 RAGSystem（懒加载单例）
        def create_rag_system():
            from src.core import RAGSystem
            config = container.get("config")
            return RAGSystem(config=config)
        
        container.register_singleton("rag_system", create_rag_system)


class MCPServiceProvider(ServiceProvider):
    """MCP服务提供者"""
    
    def register(self, container: "Container"):
        """注册MCP相关服务"""
        def create_mcp_handlers():
            from src.mcp import MCPHandlers
            rag_service = container.get("rag_service")
            return MCPHandlers(rag_service=rag_service)
        
        container.register_singleton("mcp_handlers", create_mcp_handlers)


class EventServiceProvider(ServiceProvider):
    """事件服务提供者"""
    
    def register(self, container: "Container"):
        """注册事件总线"""
        from src.events import EventBus
        
        # 注册事件总线（单例）
        container.register_singleton("event_bus", lambda: EventBus.get_instance())


class StorageServiceProvider(ServiceProvider):
    """存储服务提供者"""
    
    def register(self, container: "Container"):
        """注册存储相关服务"""
        from src.storage.strategies import StorageProvider
        
        # 注册存储提供者（单例）
        def create_storage_provider():
            provider = StorageProvider.get_instance()
            config = container.get("config")
            provider.setup(config)
            return provider
        
        container.register_singleton("storage_provider", create_storage_provider)


class ApplicationServiceProvider(ServiceProvider):
    """
    应用服务提供者
    组合所有服务提供者
    """
    
    def __init__(self):
        self._providers = [
            ConfigServiceProvider(),
            EventServiceProvider(),
            StorageServiceProvider(),
            RAGServiceProvider(),
            MCPServiceProvider(),
        ]
    
    def register(self, container: "Container"):
        """注册所有服务"""
        for provider in self._providers:
            provider.register(container)
    
    def boot(self, container: "Container"):
        """启动所有服务"""
        for provider in self._providers:
            provider.boot(container)


def setup_application(container: "Container") -> "Container":
    """
    设置应用程序
    
    Args:
        container: 容器实例
    
    Returns:
        配置好的容器实例
    """
    provider = ApplicationServiceProvider()
    provider.register(container)
    provider.boot(container)
    container.initialize()
    return container
