"""LangChain 工具 JSON Schema。"""

from __future__ import annotations

from typing import Any

from app.services.agent_tool_hints import TOOL_DESCRIPTIONS


def _fn(name: str, parameters: dict[str, Any]) -> dict[str, Any]:
    return {
        "type": "function",
        "function": {
            "name": name,
            "description": TOOL_DESCRIPTIONS.get(name, name),
            "parameters": parameters,
        },
    }


_CATEGORY = {
    "type": "string",
    "description": "分类:general|report|finding|target|note",
}

_SCHEMAS: dict[str, dict[str, Any]] = {
    "get_platform_info": _fn("get_platform_info", {"type": "object", "properties": {}}),
    "kb_list": _fn(
        "kb_list",
        {
            "type": "object",
            "properties": {
                "category": _CATEGORY,
                "keyword": {"type": "string", "description": "标题/摘要/正文关键词,可选"},
                "limit": {"type": "integer", "description": "返回条数,默认 20,最大 50"},
            },
        },
    ),
    "kb_search": _fn(
        "kb_search",
        {
            "type": "object",
            "properties": {
                "keyword": {"type": "string", "description": "搜索关键词"},
                "limit": {"type": "integer", "description": "返回条数,默认 15"},
            },
            "required": ["keyword"],
        },
    ),
    "kb_get": _fn(
        "kb_get",
        {
            "type": "object",
            "properties": {
                "entry_id": {"type": "string", "description": "知识库条目 ID"},
            },
            "required": ["entry_id"],
        },
    ),
    "kb_create": _fn(
        "kb_create",
        {
            "type": "object",
            "properties": {
                "title": {"type": "string", "description": "条目标题"},
                "content": {"type": "string", "description": "正文(支持 Markdown)"},
                "category": _CATEGORY,
                "tags": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "标签列表,如 SQL注入、内网",
                },
                "summary": {"type": "string", "description": "简短摘要,可选"},
            },
            "required": ["title", "content"],
        },
    ),
    "kb_update": _fn(
        "kb_update",
        {
            "type": "object",
            "properties": {
                "entry_id": {"type": "string", "description": "条目 ID"},
                "title": {"type": "string"},
                "content": {"type": "string"},
                "category": _CATEGORY,
                "tags": {"type": "array", "items": {"type": "string"}},
                "summary": {"type": "string"},
            },
            "required": ["entry_id"],
        },
    ),
    "kb_delete": _fn(
        "kb_delete",
        {
            "type": "object",
            "properties": {
                "entry_id": {"type": "string", "description": "要删除的条目 ID"},
            },
            "required": ["entry_id"],
        },
    ),
    "terminal_run": _fn(
        "terminal_run",
        {
            "type": "object",
            "properties": {
                "command": {
                    "type": "string",
                    "description": "要执行的 Shell 命令(可管道、子命令)",
                },
                "cwd": {
                    "type": "string",
                    "description": "沙箱内相对工作目录,默认根目录",
                },
                "timeout_seconds": {
                    "type": "integer",
                    "description": "超时秒数,默认 120,最大 300",
                },
            },
            "required": ["command"],
        },
    ),
    "bing_search": _fn(
        "bing_search",
        {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "搜索关键词或自然语言问句",
                },
                "num_results": {
                    "type": "integer",
                    "description": "返回条数,默认 8,最大 20",
                },
                "market": {
                    "type": "string",
                    "description": "区域/语言市场,如 zh-CN、en-US",
                },
            },
            "required": ["query"],
        },
    ),
    "web_fetch": _fn(
        "web_fetch",
        {
            "type": "object",
            "properties": {
                "url": {
                    "type": "string",
                    "description": "要访问的 http/https 页面地址",
                },
                "render_js": {
                    "type": "boolean",
                    "description": "是否用无头浏览器渲染 JS,默认 true",
                },
                "extract": {
                    "type": "string",
                    "description": "提取方式:text(正文,默认)或 html(原始 HTML)",
                },
            },
            "required": ["url"],
        },
    ),
    "browser": _fn(
        "browser",
        {
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "description": (
                        "操作:navigate(打开URL)、snapshot(页面结构快照)、"
                        "click、fill、press、text(正文)、close(关闭浏览器)"
                    ),
                },
                "url": {"type": "string", "description": "navigate 时的 http/https 地址"},
                "selector": {
                    "type": "string",
                    "description": "click/fill 的 CSS 选择器,如 #login、input[name=q]",
                },
                "text": {"type": "string", "description": "fill 时要输入的文本"},
                "key": {
                    "type": "string",
                    "description": "press 的按键,如 Enter、Tab",
                },
            },
            "required": ["action"],
        },
    ),
}