"""工具目录 — 按类别组织、搜索"""

from __future__ import annotations

from tool_agent.models import ToolMeta, ToolStatus


class Catalog:
    """工具目录查询"""

    def __init__(self, tools: list[ToolMeta]) -> None:
        self._tools = tools

    def by_category(self) -> dict[str, list[ToolMeta]]:
        """按类别分组"""
        result: dict[str, list[ToolMeta]] = {}
        for t in self._tools:
            if t.status == ToolStatus.ACTIVE:
                result.setdefault(t.category or "uncategorized", []).append(t)
        return result

    def search(self, keyword: str) -> list[ToolMeta]:
        """关键词搜索"""
        keyword = keyword.lower()
        return [
            t for t in self._tools
            if t.status == ToolStatus.ACTIVE
            and (keyword in t.name.lower() or keyword in t.description.lower())
        ]
