"""Plugin Management API Endpoints.

This router provides comprehensive plugin discovery, monitoring, and management functionality.
It allows administrators to view plugin status, retrieve detailed information about available plugins,
and reload the entire plugin system when updates are deployed.
"""

from __future__ import annotations

from typing import List

from fastapi import APIRouter, Depends, HTTPException

from ...infrastructure.plugins.sdk_manager import SDKPluginManager
from ..schemas import PluginInfo
from ..services import global_service_container

plugins_api_router = APIRouter()


def get_plugin_manager() -> SDKPluginManager:
    """Dependency injection function to retrieve the plugin manager from the global service container."""
    return global_service_container.get_plugin_manager()


@plugins_api_router.get("/plugins", response_model=List[PluginInfo])
async def list_available_plugins(plugin_manager: SDKPluginManager = Depends(get_plugin_manager)) -> List[PluginInfo]:
    """Retrieve comprehensive information about all discovered plugins in the system.

    Returns a list of all plugins with their metadata, capabilities, and current health status
    for system monitoring and administration purposes.
    """
    discovered_plugins: List[PluginInfo] = []

    for plugin_identifier in plugin_manager.get_available_plugins():
        plugin_bundle = plugin_manager.get_plugin_bundle(plugin_identifier)
        if plugin_bundle:
            plugin_metadata = plugin_bundle.metadata
            plugin_status = "healthy" if plugin_identifier in plugin_manager.healthy_plugins else "failed"

            discovered_plugins.append(
                PluginInfo(
                    name=plugin_metadata.name,
                    version=plugin_metadata.version,
                    description=plugin_metadata.description,
                    capabilities=plugin_metadata.capabilities,
                    status=plugin_status,
                )
            )

    return discovered_plugins


@plugins_api_router.get("/plugins/{plugin_name}", response_model=PluginInfo)
async def get_plugin_details(
    plugin_name: str, plugin_manager: SDKPluginManager = Depends(get_plugin_manager)
) -> PluginInfo:
    """Retrieve detailed metadata and operational status for a specific plugin.

    Provides comprehensive information about a single plugin including its capabilities,
    version details, and current health status for troubleshooting and monitoring.
    """
    plugin_bundle = plugin_manager.get_plugin_bundle(plugin_name)
    if not plugin_bundle:
        raise HTTPException(status_code=404, detail=f"Plugin '{plugin_name}' not found in available plugins")

    plugin_metadata = plugin_bundle.metadata
    plugin_status = "healthy" if plugin_name in plugin_manager.healthy_plugins else "failed"

    return PluginInfo(
        name=plugin_metadata.name,
        version=plugin_metadata.version,
        description=plugin_metadata.description,
        capabilities=plugin_metadata.capabilities,
        status=plugin_status,
    )


@plugins_api_router.post("/plugins/reload")
async def reload_all_plugins(plugin_manager: SDKPluginManager = Depends(get_plugin_manager)) -> dict:
    """Reload the entire plugin system and rebuild the orchestrator graph.

    This endpoint triggers a complete plugin reload, which is useful after deploying
    new plugin versions or when troubleshooting plugin-related issues. The orchestrator
    graph is automatically rebuilt to incorporate any changes.
    """
    try:
        plugin_manager.reload_plugins()
        try:
            orchestrator = global_service_container.get_orchestrator()
            orchestrator.rebuild_graph()
        except Exception as e:
            plugins_api_router.logger.warning(f"Failed to rebuild orchestrator graph after reload: {e}")
        return {
            "status": "success",
            "loaded": list(plugin_manager.get_available_plugins()),
            "healthy": list(plugin_manager.healthy_plugins),
            "failed": list(plugin_manager.failed_plugins),
        }
    except Exception as reload_error:
        raise HTTPException(status_code=500, detail=f"Plugin reload failed: {str(reload_error)}")


router = plugins_api_router
