"""
数据源管理工具 (工具 1-6)
"""

import logging

from ._base import register_tool, ToolDef

logger = logging.getLogger(__name__)


@register_tool("list_datasources")
class ListDatasourcesTool(ToolDef):
    name = "list_datasources"
    description = "获取数据源列表，支持搜索和状态筛选"
    input_schema = {
        "type": "object",
        "properties": {
            "datasourceName": {"type": "string", "description": "数据源名称模糊搜索"},
            "status": {"type": "integer", "description": "0=运行中, 1=已停止, 不传=全部"},
            "sourceType": {"type": "string", "enum": ["builtin", "external"], "description": "数据源类型"},
            "pageNum": {"type": "integer", "default": 1, "description": "页码"},
            "pageSize": {"type": "integer", "default": 20, "description": "每页数量"},
        },
        "required": [],
    }

    async def execute(self, args: dict) -> dict:
        params = {k: v for k, v in args.items() if v is not None}
        return self.client.get("/api/datasource/connection/list", params=params)


@register_tool("get_datasource_detail")
class GetDatasourceDetailTool(ToolDef):
    name = "get_datasource_detail"
    description = "获取单个数据源的完整详情（含配置和实时结构）"
    input_schema = {
        "type": "object",
        "properties": {
            "datasourceId": {"type": "string", "description": "数据源 ID"},
        },
        "required": ["datasourceId"],
    }

    async def execute(self, args: dict) -> dict:
        ds_id = args["datasourceId"]
        result = {}
        # 获取基本信息
        try:
            result["detail"] = self.client.get(f"/api/datasource/connection/{ds_id}")
        except Exception as e:
            result["detail"] = {"error": str(e)}
        # 获取配置
        try:
            result["config"] = self.client.get(f"/api/datasource/config/{ds_id}")
        except Exception as e:
            result["config"] = {"error": str(e)}
        # 获取实时结构
        try:
            result["structure"] = self.client.get(f"/api/datasource/connection/realtime/structure/{ds_id}")
        except Exception as e:
            result["structure"] = {"error": str(e)}
        return result


@register_tool("create_datasource")
class CreateDatasourceTool(ToolDef):
    name = "create_datasource"
    description = "创建外部数据源连接（可选先测试连接）"
    input_schema = {
        "type": "object",
        "properties": {
            "datasourceName": {"type": "string", "description": "数据源名称（3-20字）"},
            "datasourceType": {"type": "string", "enum": ["mysql", "postgresql", "oracle", "sqlserver", "dameng"], "description": "数据库类型"},
            "host": {"type": "string", "description": "数据库地址"},
            "port": {"type": "integer", "description": "端口号"},
            "databaseName": {"type": "string", "description": "要连接的数据库名"},
            "username": {"type": "string", "description": "数据库用户名"},
            "password": {"type": "string", "description": "密码"},
            "remark": {"type": "string", "description": "数据源描述"},
            "connectionType": {"type": "string", "enum": ["user_password", "ssl"], "description": "连接类型，默认 user_password"},
            "test_first": {"type": "boolean", "default": True, "description": "是否先测试连接，默认 true"},
        },
        "required": ["datasourceName", "datasourceType", "host", "port", "databaseName", "username"],
    }

    async def execute(self, args: dict) -> dict:
        args = dict(args)
        test_first = args.pop("test_first", True)
        
        # 如果需要先测试连接
        if test_first:
            test_data = {
                "datasourceName": args.get("datasourceName"),
                "datasourceType": args.get("datasourceType"),
                "host": args.get("host"),
                "port": args.get("port"),
                "databaseName": args.get("databaseName"),
                "username": args.get("username"),
                "password": args.get("password"),
                "connectionType": args.get("connectionType", "user_password"),
            }
            test_result = self.client.post("/api/datasource/connection/test", json_data=test_data)
            if test_result.get("code") != 200:
                return {"success": False, "error": f"连接测试失败: {test_result.get('msg', '未知错误')}"}
        
        # 创建数据源
        return self.client.post("/api/datasource/connection", json_data=args)


@register_tool("update_datasource")
class UpdateDatasourceTool(ToolDef):
    name = "update_datasource"
    description = "更新数据源连接信息"
    input_schema = {
        "type": "object",
        "properties": {
            "id": {"type": "string", "description": "数据源 ID"},
            "datasourceName": {"type": "string", "description": "更新名称"},
            "host": {"type": "string", "description": "更新地址"},
            "port": {"type": "integer", "description": "更新端口"},
            "databaseName": {"type": "string", "description": "更新数据库名"},
            "username": {"type": "string", "description": "更新用户名"},
            "password": {"type": "string", "description": "新密码（不传则不变）"},
            "remark": {"type": "string", "description": "更新描述"},
        },
        "required": ["id"],
    }

    async def execute(self, args: dict) -> dict:
        return self.client.put("/api/datasource/connection", json_data=args)


@register_tool("toggle_datasource_status")
class ToggleDatasourceStatusTool(ToolDef):
    name = "toggle_datasource_status"
    description = "启用/停用数据源"
    input_schema = {
        "type": "object",
        "properties": {
            "id": {"type": "string", "description": "数据源 ID"},
            "status": {"type": "integer", "enum": [0, 1], "description": "0=启用, 1=停用"},
        },
        "required": ["id", "status"],
    }

    async def execute(self, args: dict) -> dict:
        return self.client.put("/api/datasource/connection/changeStatus", json_data=args)


@register_tool("delete_datasource")
class DeleteDatasourceTool(ToolDef):
    name = "delete_datasource"
    description = "删除数据源（运行中会自动先停用）"
    input_schema = {
        "type": "object",
        "properties": {
            "id": {"type": "string", "description": "数据源 ID"},
        },
        "required": ["id"],
    }

    async def execute(self, args: dict) -> dict:
        ds_id = args["id"]
        # 先尝试停用（仅忽略已停用等预期错误）
        try:
            self.client.put("/api/datasource/connection/changeStatus", json_data={"id": ds_id, "status": 1})
        except Exception as e:
            # 记录日志但继续删除
            logger.debug(f"停用数据源失败（可能已停用）: {e}")
        
        # 删除数据源
        return self.client.delete(f"/api/datasource/connection/{ds_id}")
