"""Tools API Schemas"""

from pydantic import BaseModel
from typing import Optional


class ToolCommand(BaseModel):
    """单个工具命令"""

    name: str
    description: str
    parameters: dict = {}


class ToolStats(BaseModel):
    """工具调用统计"""

    call_count: int = 0
    error_count: int = 0
    total_duration_ms: int = 0
    avg_duration_ms: int = 0
    error_rate: float = 0.0


class ToolInfo(BaseModel):
    """工具完整信息"""

    name: str
    display_name: str
    description: str
    category: str  # 中文分类名
    subcategory: str = ""  # 子分类
    source: str  # "manifest" | "agent"
    source_file: str = ""  # 来源文件路径
    tags: list[str] = []
    enabled: bool = True
    commands: list[ToolCommand] = []
    stats: Optional[ToolStats] = None
    config: dict = {}  # 配置参数（只读）


class ToolCategory(BaseModel):
    """工具分类"""

    name: str  # 大分类：注册层 / Agent层
    display_name: str  # 中文显示名
    subgroups: list["ToolSubgroup"] = []


class ToolSubgroup(BaseModel):
    """工具子分类"""

    name: str  # 子分类名
    display_name: str  # 中文显示名
    tools: list[ToolInfo] = []


class ToolSummary(BaseModel):
    """工具统计摘要"""

    total_commands: int = 0
    active_commands: int = 0
    total_tools: int = 0
    manifest_tools: int = 0
    agent_tools: int = 0


class ToolsResponse(BaseModel):
    """GET /api/tools 响应"""

    categories: list[ToolCategory]
    summary: ToolSummary


# 更新前向引用
ToolCategory.model_rebuild()
