"""
库表关联配置工具（外置/内置通用）

对应文档 §2.2 / §4.2：
- postConnectionConfig  建库表关联配置（外置源建完连接后选库选表的最后一步）
- putConnectionConfig   改库表关联配置
- deleteConnectionConfig 删配置（同时也是「删除库」的实现）
"""

from ._base import register_tool, ToolDef


@register_tool("create_connection_config")
class CreateConnectionConfigTool(ToolDef):
    name = "create_connection_config"
    description = (
        "创建「库表关联」配置：外置数据源建完连接后，选择要纳管的库与表的最后一步。"
        "完成后数据源才能用于智能问数"
    )
    input_schema = {
        "type": "object",
        "properties": {
            "connectionId": {"type": "string", "description": "数据源（连接）ID"},
            "syncTables": {"type": "boolean", "default": True, "description": "是否同步表结构，默认 true"},
            "databases": {
                "type": "array",
                "description": "要纳管的库及其表",
                "items": {
                    "type": "object",
                    "properties": {
                        "databaseName": {"type": "string", "description": "库名"},
                        "tableNames": {
                            "type": "array",
                            "description": "选中的表名数组",
                            "items": {"type": "string"},
                        },
                    },
                    "required": ["databaseName", "tableNames"],
                },
            },
        },
        "required": ["connectionId", "databases"],
    }

    async def execute(self, args: dict) -> dict:
        args = dict(args)
        args.setdefault("syncTables", True)
        return await self.client.post("/datasource/config", json_data=args)


@register_tool("update_connection_config")
class UpdateConnectionConfigTool(ToolDef):
    name = "update_connection_config"
    description = "更新「库表关联」配置（编辑已纳管的库表范围）"
    input_schema = {
        "type": "object",
        "properties": {
            "id": {"type": "string", "description": "配置 ID（编辑时必传）"},
            "connectionId": {"type": "string", "description": "数据源（连接）ID"},
            "syncTables": {"type": "boolean", "default": True, "description": "是否同步表结构，默认 true"},
            "databases": {
                "type": "array",
                "description": "要纳管的库及其表",
                "items": {
                    "type": "object",
                    "properties": {
                        "databaseName": {"type": "string", "description": "库名"},
                        "tableNames": {
                            "type": "array",
                            "description": "选中的表名数组",
                            "items": {"type": "string"},
                        },
                    },
                    "required": ["databaseName", "tableNames"],
                },
            },
        },
        "required": ["id", "connectionId", "databases"],
    }

    async def execute(self, args: dict) -> dict:
        args = dict(args)
        args.setdefault("syncTables", True)
        return await self.client.put("/datasource/config", json_data=args)


@register_tool("delete_connection_config")
class DeleteConnectionConfigTool(ToolDef):
    name = "delete_connection_config"
    description = "删除库表关联配置（批量）。这也是「删除内置库」的实现：传入对应库的配置 ID"
    input_schema = {
        "type": "object",
        "properties": {
            "ids": {
                "type": "array",
                "description": "配置 ID 数组",
                "items": {"type": "string"},
            },
        },
        "required": ["ids"],
    }

    async def execute(self, args: dict) -> dict:
        return await self.client.post("/datasource/config/deletes", json_data={"ids": args["ids"]})
