"""插件 Skills 加载器 - Phase 9.2"""

from typing import Any

from app.agents.skills.metadata import SkillMetadata
from app.agents.plugins.manager import get_plugin_manager


class PluginSkillLoader:
    """插件 Skills 加载器

    从已安装的插件中加载 Skills。
    """

    def __init__(self):
        self.plugin_manager = get_plugin_manager()

    def load_all(self) -> list[SkillMetadata]:
        """从所有已启用的插件加载 Skills

        Returns:
            Skill 元数据列表
        """
        skills = []

        for plugin in self.plugin_manager.list_plugins():
            if not self.plugin_manager.is_enabled(plugin.id):
                continue

            # 从插件加载 Skills
            plugin_skills = self._load_from_plugin(plugin)
            skills.extend(plugin_skills)

        return skills

    def _load_from_plugin(self, plugin: Any) -> list[SkillMetadata]:
        """从单个插件加载 Skills"""
        skills = []

        for skill_name in plugin.skills:
            skill = SkillMetadata(
                name=f"{plugin.id}/{skill_name}",
                description=f"Skill from plugin: {plugin.name}",
                version=plugin.version,
                author=plugin.author,
                tags=["plugin", plugin.id],
                content=f"# {skill_name}\n\nFrom plugin: {plugin.name}",
                source="plugin",
                source_id=plugin.id,
            )
            skills.append(skill)

        return skills
