from __future__ import annotations

from collections.abc import Callable
from typing import Any

PluginHandler = Callable[[dict[str, Any]], dict[str, Any]]


class PluginRegistry:
    def __init__(self) -> None:
        self._handlers: dict[str, PluginHandler] = {}

    def register(self, plugin_id: str, handler: PluginHandler) -> None:
        self._handlers[plugin_id] = handler

    def execute(self, plugin_id: str, slots: dict[str, Any]) -> dict[str, Any]:
        handler = self._handlers.get(plugin_id)
        if handler is None:
            return {
                "success": False,
                "message": f"插件 {plugin_id} 未注册。",
                "data": {"plugin_id": plugin_id, "slots": slots},
            }
        return handler(slots)

    def registered_plugins(self) -> list[str]:
        return sorted(self._handlers.keys())
