"""工具元数据和数据类型定义"""

from dataclasses import dataclass, field
from enum import Enum
from typing import Any


class ToolCategory(Enum):
    """工具类别"""

    READ = "read"
    WRITE = "write"
    EXTERNAL = "external"
    DB_WRITE = "db_write"
    NETWORK = "network"


class SideEffectScope(Enum):
    """副作用范围"""

    NONE = "none"
    LOCAL_STATE = "local_state"
    DB_WRITE = "db_write"
    NETWORK = "network"


class PermissionClass(Enum):
    """权限级别"""

    READ = "read"
    WRITE = "write"
    EXTERNAL = "external"


@dataclass
class ToolManifest:
    """工具元数据"""

    name: str
    description: str
    category: ToolCategory
    parameters: dict[str, Any]  # JSON Schema
    return_schema: dict[str, Any]
    permission_class: PermissionClass
    side_effect_scope: SideEffectScope
    requires_confirmation: bool = False
    is_streaming: bool = False
    tags: list[str] = field(default_factory=list)

    def to_dict(self) -> dict[str, Any]:
        return {
            "name": self.name,
            "description": self.description,
            "category": self.category.value,
            "parameters": self.parameters,
            "return_schema": self.return_schema,
            "permission_class": self.permission_class.value,
            "side_effect_scope": self.side_effect_scope.value,
            "requires_confirmation": self.requires_confirmation,
            "is_streaming": self.is_streaming,
            "tags": self.tags,
        }


@dataclass
class HookConfig:
    """Hook 配置"""

    name: str
    hook_type: str  # "pre_tool_use", "post_tool_use", "tool_error", "tool_skip"
    filter_names: list[str] | None = None  # 只对特定工具生效，None 表示全部

    def matches_tool(self, tool_name: str) -> bool:
        """检查 Hook 是否对指定工具生效"""
        if self.filter_names is None:
            return True
        return tool_name in self.filter_names
