"""
Tool 组件包

自动导入所有工具模块，实现工具的自动发现和注册
"""

import os
import importlib
from pathlib import Path


def auto_import_tools():
    """自动导入当前包下的所有 Python 模块"""
    # 获取当前包的路径
    current_dir = Path(__file__).parent
    
    # 获取所有 Python 文件（排除 __init__.py 和 base.py）
    tool_files = [
        f.stem for f in current_dir.glob("*.py") 
        if f.name not in ["__init__.py", "base.py"] and not f.name.startswith("_")
    ]
    
    imported_modules = []
    failed_imports = []
    
    for module_name in tool_files:
        try:
            # 动态导入模块
            module = importlib.import_module(f".{module_name}", package=__name__)
            imported_modules.append(module_name)
            
            # 可选：添加到当前命名空间
            globals()[module_name] = module
            
        except Exception as e:
            failed_imports.append((module_name, str(e)))
    
    # 输出导入结果（仅在开发模式下）
    if os.getenv("DEBUG_TOOL_IMPORT", "").lower() in ["true", "1", "yes"]:
        print(f"✅ Successfully imported {len(imported_modules)} tool modules: {imported_modules}")
        if failed_imports:
            print(f"❌ Failed to import {len(failed_imports)} modules:")
            for module_name, error in failed_imports:
                print(f"  - {module_name}: {error}")
    
    return imported_modules, failed_imports


# 执行自动导入
auto_import_tools()

# 导出基础组件
from .base import (
    BaseToolComponent,
    SimpleTool,
    AsyncTool,
    ConfigurableTool,
    ToolRegistry,
    tool_registry,
    component_tool
)

__all__ = [
    "BaseToolComponent",
    "SimpleTool", 
    "AsyncTool",
    "ConfigurableTool",
    "ToolRegistry",
    "tool_registry",
    "component_tool",
    "auto_import_tools"
]