"""Agent端技能模块 - 兼容独立运行和包导入"""

import sys
import os

# 确保 agent/ 目录在路径中
_agent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if _agent_dir not in sys.path:
    sys.path.insert(0, _agent_dir)


def _build_skill_registry():
    """延迟构建技能注册表（供 SkillExecutor 使用）"""
    from skills.wechat.skill import WechatSkill
    from skills.douyin.skill import DouyinSkill
    from skills.xhs.skill import XhsSkill
    from skills.xianyu.skill import XianyuSkill
    from skills.soul.skill import SoulSkill
    from skills.voice_control import VoiceControlSkill
    from skills.app_manager import AppManagerSkill
    from skills.search import SearchSkill
    from skills.network_reconnect import NetworkReconnectSkill
    from skills.connection_guard import ConnectionGuardSkill
    return {
        "wechat": WechatSkill,
        "douyin": DouyinSkill,
        "xhs": XhsSkill,
        "xianyu": XianyuSkill,
        "soul": SoulSkill,
        "voice_control": VoiceControlSkill,
        "app_manager": AppManagerSkill,
        "search": SearchSkill,
        "network_reconnect": NetworkReconnectSkill,
        "connection_guard": ConnectionGuardSkill,
    }


# 延迟填充，首次访问时构建
SKILL_REGISTRY = {}


def _ensure_registry():
    global SKILL_REGISTRY
    if not SKILL_REGISTRY:
        try:
            SKILL_REGISTRY.update(_build_skill_registry())
        except ImportError as e:
            import logging
            logging.getLogger(__name__).warning(f"部分技能加载失败（可忽略）: {e}")


def get_skill(name: str):
    """获取技能类"""
    if name == "wechat":
        from skills.wechat.skill import WechatSkill
        return WechatSkill
    elif name == "douyin":
        from skills.douyin.skill import DouyinSkill
        return DouyinSkill
    elif name == "xhs":
        from skills.xhs.skill import XhsSkill
        return XhsSkill
    elif name == "xianyu":
        from skills.xianyu.skill import XianyuSkill
        return XianyuSkill
    elif name == "soul":
        from skills.soul.skill import SoulSkill
        return SoulSkill
    else:
        raise ImportError(f"未知技能: {name}")


# 导出供 skill_executor 使用
from skills.voice_control import VoiceControlSkill
from skills.app_manager import AppManagerSkill
from skills.search import SearchSkill
from skills.network_reconnect import NetworkReconnectSkill
from skills.connection_guard import ConnectionGuardSkill

_ensure_registry()
