"""MCP (Model Context Protocol) plugin.

Provides slash commands and status views for MCP server management:
- /mcp show - Display MCP status panel
- /mcp list - List all MCP servers and tools
- /mcp servers - Show connected servers
- /mcp tools - Show available tools
"""

import logging
from typing import Dict, Any, List

from core.io.visual_effects import AgnosterSegment
from core.io.status.mcp_status_view import MCPStatusView

logger = logging.getLogger(__name__)


class MCPPlugin:
    """Plugin for MCP (Model Context Protocol) integration."""

    def __init__(
        self,
        name: str = "mcp",
        event_bus=None,
        renderer=None,
        config=None
    ) -> None:
        """Initialize the MCP plugin.

        Args:
            name: Plugin name.
            event_bus: Event bus for event handling.
            renderer: Terminal renderer.
            config: Configuration manager.
        """
        self.name = name
        self.version = "1.0.0"
        self.description = "MCP server management and status display"
        self.enabled = True
        self.event_bus = event_bus
        self.renderer = renderer
        self.config = config
        self.logger = logger
        self.mcp_integration = None
        self.mcp_command_handler = None

    async def initialize(self, event_bus, config, **kwargs) -> None:
        """Initialize the plugin.

        Args:
            event_bus: Event bus instance
            config: Configuration instance
            **kwargs: Additional kwargs (may include renderer, app, etc.)
        """
        try:
            self.event_bus = event_bus
            self.config = config
            self.renderer = kwargs.get('renderer')
            llm_service = kwargs.get('llm_service')
            command_registry = kwargs.get('command_registry')

            # Get MCP integration from llm_service if available
            if llm_service and hasattr(llm_service, 'mcp_integration'):
                self.mcp_integration = llm_service.mcp_integration

            # Register slash commands
            await self._register_commands(command_registry)

            # Register status view
            await self._register_status_view()

            self.logger.info("MCP plugin initialized successfully")
        except Exception as e:
            self.logger.error(f"Error initializing MCP plugin: {e}")
            raise

    async def _register_commands(self, command_registry) -> None:
        """Register MCP slash commands.

        Args:
            command_registry: Command registry instance from kwargs
        """
        try:
            from core.commands.mcp_command import register_mcp_commands

            if command_registry:
                # Register commands even without MCP integration - users need /mcp setup
                self.mcp_command_handler = register_mcp_commands(
                    command_registry=command_registry,
                    mcp_integration=self.mcp_integration,  # Can be None
                    renderer=self.renderer,
                    app=None  # Not needed by command handler
                )
                self.logger.info("MCP commands registered")
            else:
                self.logger.warning("Command registry not found, skipping MCP command registration")

        except Exception as e:
            self.logger.error(f"Failed to register MCP commands: {e}")

    async def _register_status_view(self) -> None:
        """Register MCP status view in status area."""
        try:
            if not self.mcp_integration:
                self.logger.warning("MCP integration not available, skipping status view registration")
                return

            if (self.renderer and
                hasattr(self.renderer, 'status_renderer') and
                self.renderer.status_renderer and
                hasattr(self.renderer.status_renderer, 'status_registry') and
                self.renderer.status_renderer.status_registry):

                from core.io.status_renderer import StatusViewConfig, BlockConfig

                view = StatusViewConfig(
                    name="MCP Status",
                    plugin_source="mcp",
                    priority=450,  # Lower priority than system commands
                    blocks=[BlockConfig(
                        width_fraction=1.0,
                        content_provider=self._get_status_content,
                        title="MCP",
                        priority=100
                    )],
                )

                registry = self.renderer.status_renderer.status_registry
                registry.register_status_view("mcp", view)
                self.logger.info("Registered 'MCP Status' status view")

        except Exception as e:
            self.logger.error(f"Failed to register MCP status view: {e}")

    def _get_status_content(self) -> List[str]:
        """Get MCP status content for status bar display.

        Returns:
            List of formatted status lines
        """
        try:
            if not self.mcp_integration:
                seg = AgnosterSegment()
                seg.add_neutral("MCP: N/A", "dark")
                return [seg.render()]

            connections = self.mcp_integration.server_connections
            tool_registry = self.mcp_integration.tool_registry

            # Count connected servers and tools
            connected_count = len([
                c for c in connections.values()
                if c.initialized
            ])
            tool_count = len(tool_registry)

            seg = AgnosterSegment()

            if connected_count > 0:
                seg.add_lime("MCP", "dark")
                seg.add_cyan(f"{connected_count} srv", "dark")
                seg.add_neutral(f"{tool_count} tools", "mid")
            else:
                seg.add_neutral("MCP: offline", "dark")

            return [seg.render()]

        except Exception as e:
            self.logger.error(f"Error getting MCP status content: {e}")
            seg = AgnosterSegment()
            seg.add_neutral("MCP: Error", "dark")
            return [seg.render()]

    async def register_hooks(self) -> None:
        """Register event hooks.

        The MCP integration itself emits events, so we don't need
        additional hooks here unless we want to react to MCP events.
        """
        pass

    async def shutdown(self) -> None:
        """Shutdown the plugin."""
        self.logger.info("MCP plugin shutdown completed")

    @staticmethod
    def get_default_config() -> Dict[str, Any]:
        """Get default configuration.

        Returns:
            Default configuration dictionary
        """
        return {
            "plugins": {
                "mcp": {
                    "enabled": True,
                    "status_display": True,
                    "commands": {
                        "enabled": True
                    }
                }
            }
        }
