"""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 kollabor_tui.visual_effects import AgnosterSegment
from kollabor_tui.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)

            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 kollabor.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}")

    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 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},
                }
            }
        }
