"""MCP tool definitions for datasource API."""

from typing import Any, Dict, List


def _tool(
    name: str,
    description: str,
    method: str,
    path: str,
    properties: Dict[str, Any],
    required: List[str],
    param_categories: Dict[str, str],
) -> Dict[str, Any]:
    """Build a tool definition dictionary."""
    return {
        "name": name,
        "description": description,
        "method": method,
        "path": path,
        "inputSchema": {
            "type": "object",
            "properties": properties,
            "required": required,
        },
        "paramCategories": param_categories,
    }


# 公共参数 schema 片段
_API_KEY_PROP = {
    "apiKey": {
        "type": "string",
        "description": "可选的 X-Datasource-API-Key 权限密钥",
    }
}

_TARGET_PROP = {
    "target": {
        "type": "string",
        "description": "目标环境（prod/test，默认 prod）",
        "default": "prod",
    }
}

_PAGE_PROPS = {
    "pageNum": {"type": "integer", "description": "页码（默认1）", "default": 1},
    "pageSize": {"type": "integer", "description": "每页数量（默认10）", "default": 10},
}

# =============================================================================
# 1. 数据源配置管理
# =============================================================================

LIST_DATASOURCE_CONFIGS = _tool(
    name="list_datasource_configs",
    description="查询数据源配置列表，支持分页和多条件筛选",
    method="GET",
    path="/datasource/config/list",
    properties={
        "datasourceName": {"type": "string", "description": "数据源名称（模糊查询）"},
        "datasourceId": {"type": "integer", "description": "连接ID"},
        "datasourceType": {"type": "string", "description": "数据库类型"},
        "status": {"type": "integer", "description": "状态"},
        "showTable": {"type": "boolean", "description": "是否显示表数量"},
        **_PAGE_PROPS,
    },
    required=[],
    param_categories={
        "datasourceName": "query",
        "datasourceId": "query",
        "datasourceType": "query",
        "status": "query",
        "showTable": "query",
        "pageNum": "query",
        "pageSize": "query",
    },
)

GET_DATASOURCE_CONFIG = _tool(
    name="get_datasource_config",
    description="获取指定数据源配置的详细信息",
    method="GET",
    path="/datasource/config/{id}",
    properties={
        "id": {"type": "integer", "description": "数据源配置ID"},
    },
    required=["id"],
    param_categories={"id": "path"},
)

BATCH_CREATE_DATASOURCE_CONFIGS = _tool(
    name="batch_create_datasource_configs",
    description="批量创建数据源配置并同步表结构",
    method="POST",
    path="/datasource/config",
    properties={
        "connectionId": {"type": "integer", "description": "连接实例ID"},
        "datasourceNamePrefix": {"type": "string", "description": "数据源名称前缀"},
        "enterpriseId": {"type": "integer", "description": "企业ID"},
        "status": {"type": "integer", "description": "状态"},
        "remark": {"type": "string", "description": "备注信息"},
        "syncTables": {"type": "boolean", "description": "是否同步表结构"},
        "databases": {
            "type": "array",
            "description": "数据库与表列表",
            "items": {
                "type": "object",
                "properties": {
                    "databaseName": {"type": "string"},
                    "tableNames": {"type": "array", "items": {"type": "string"}},
                },
            },
        },
    },
    required=["connectionId", "databases"],
    param_categories={
        "connectionId": "body",
        "datasourceNamePrefix": "body",
        "enterpriseId": "body",
        "status": "body",
        "remark": "body",
        "syncTables": "body",
        "databases": "body",
    },
)

REPLACE_DATASOURCE_CONFIGS = _tool(
    name="replace_datasource_configs",
    description="全量替换指定连接下的数据源配置（未传入的配置将被删除），请谨慎操作",
    method="PUT",
    path="/datasource/config",
    properties={
        "connectionId": {"type": "integer", "description": "连接实例ID"},
        "datasourceNamePrefix": {"type": "string", "description": "数据源名称前缀"},
        "enterpriseId": {"type": "integer", "description": "企业ID"},
        "status": {"type": "integer", "description": "状态"},
        "remark": {"type": "string", "description": "备注信息"},
        "syncTables": {"type": "boolean", "description": "是否同步表结构"},
        "databases": {
            "type": "array",
            "description": "数据库与表列表",
            "items": {
                "type": "object",
                "properties": {
                    "databaseName": {"type": "string"},
                    "tableNames": {"type": "array", "items": {"type": "string"}},
                },
            },
        },
    },
    required=["connectionId", "databases"],
    param_categories={
        "connectionId": "body",
        "datasourceNamePrefix": "body",
        "enterpriseId": "body",
        "status": "body",
        "remark": "body",
        "syncTables": "body",
        "databases": "body",
    },
)

BATCH_UPDATE_DATASOURCE_CONFIGS = _tool(
    name="batch_update_datasource_configs",
    description="批量修改数据源配置并重新同步表结构",
    method="PUT",
    path="/datasource/config/batch",
    properties={
        "status": {"type": "integer", "description": "状态"},
        "remark": {"type": "string", "description": "批量更新备注"},
        "syncTables": {"type": "boolean", "description": "是否同步表结构"},
        "datasources": {
            "type": "array",
            "description": "要更新的数据源配置列表",
            "items": {
                "type": "object",
                "properties": {
                    "id": {"type": "integer"},
                    "datasourceName": {"type": "string"},
                    "tableNames": {"type": "array", "items": {"type": "string"}},
                },
            },
        },
    },
    required=["datasources"],
    param_categories={
        "status": "body",
        "remark": "body",
        "syncTables": "body",
        "datasources": "body",
    },
)

DELETE_DATASOURCE_CONFIGS = _tool(
    name="delete_datasource_configs",
    description="批量删除数据源配置",
    method="POST",
    path="/datasource/config/deletes",
    properties={
        "ids": {
            "type": "array",
            "description": "要删除的数据源配置ID列表",
            "items": {"type": "integer"},
        },
    },
    required=["ids"],
    param_categories={"ids": "body"},
)

TEST_CONNECTION_CONFIG = _tool(
    name="test_connection_config",
    description="测试数据库连接是否正常",
    method="POST",
    path="/datasource/config/testConnectionConfig",
    properties={
        "host": {"type": "string", "description": "主机地址"},
        "port": {"type": "integer", "description": "端口"},
        "username": {"type": "string", "description": "用户名"},
        "password": {"type": "string", "description": "密码"},
        "datasourceType": {"type": "string", "description": "数据库类型，例如 PostgreSQL（默认 PostgreSQL）", "default": "PostgreSQL"},
    },
    required=["host", "port", "username", "password"],
    param_categories={
        "host": "body",
        "port": "body",
        "username": "body",
        "password": "body",
        "datasourceType": "body",
    },
)

CHANGE_DATASOURCE_STATUS = _tool(
    name="change_datasource_status",
    description="修改数据源配置的启用/禁用状态",
    method="PUT",
    path="/datasource/config/changeStatus",
    properties={
        "id": {"type": "integer", "description": "数据源配置ID"},
        "status": {"type": "integer", "description": "状态（0正常/1停用）"},
    },
    required=["id", "status"],
    param_categories={"id": "body", "status": "body"},
)

EXPORT_DATASOURCE_CONFIGS = _tool(
    name="export_datasource_configs",
    description="导出数据源配置列表为Excel文件",
    method="POST",
    path="/datasource/config/export",
    properties={
        "datasourceName": {"type": "string", "description": "数据源名称（模糊查询）"},
        "datasourceId": {"type": "integer", "description": "连接ID"},
        "datasourceType": {"type": "string", "description": "数据库类型"},
        "status": {"type": "integer", "description": "状态"},
    },
    required=[],
    param_categories={
        "datasourceName": "query",
        "datasourceId": "query",
        "datasourceType": "query",
        "status": "query",
    },
)

# =============================================================================
# 2. 数据库连接实例管理
# =============================================================================

LIST_CONNECTIONS = _tool(
    name="list_connections",
    description="查询数据库连接实例列表，支持分页和筛选",
    method="GET",
    path="/datasource/connection/list",
    properties={
        "datasourceName": {"type": "string", "description": "数据源名称（模糊查询）"},
        "status": {"type": "integer", "description": "状态"},
        "testStatus": {"type": "integer", "description": "测试状态（0未测试/1成功/2失败）"},
        "sourceType": {"type": "string", "description": "连接来源（builtin/external）"},
        **_PAGE_PROPS,
    },
    required=[],
    param_categories={
        "datasourceName": "query",
        "status": "query",
        "testStatus": "query",
        "sourceType": "query",
        "pageNum": "query",
        "pageSize": "query",
    },
)

GET_CONNECTION = _tool(
    name="get_connection",
    description="获取指定连接实例的详细信息",
    method="GET",
    path="/datasource/connection/{id}",
    properties={
        "id": {"type": "integer", "description": "连接实例ID"},
    },
    required=["id"],
    param_categories={"id": "path"},
)

CREATE_CONNECTION = _tool(
    name="create_connection",
    description="创建新的数据库连接实例",
    method="POST",
    path="/datasource/connection",
    properties={
        "datasourceName": {"type": "string", "description": "连接名称"},
        "datasourceType": {"type": "string", "description": "数据库类型，例如 PostgreSQL（默认 PostgreSQL）", "default": "PostgreSQL"},
        "connectionType": {"type": "string", "description": "连接方式，例如 user_password（默认 user_password）", "default": "user_password"},
        "host": {"type": "string", "description": "主机地址"},
        "port": {"type": "integer", "description": "端口"},
        "username": {"type": "string", "description": "用户名"},
        "password": {"type": "string", "description": "密码"},
        "status": {"type": "integer", "description": "状态"},
        "remark": {"type": "string", "description": "备注信息"},
    },
    required=["datasourceName", "host", "port", "username", "password"],
    param_categories={
        "datasourceName": "body",
        "datasourceType": "body",
        "connectionType": "body",
        "host": "body",
        "port": "body",
        "username": "body",
        "password": "body",
        "status": "body",
        "remark": "body",
    },
)

UPDATE_CONNECTION = _tool(
    name="update_connection",
    description="修改数据库连接实例信息",
    method="PUT",
    path="/datasource/connection",
    properties={
        "id": {"type": "integer", "description": "连接实例ID"},
        "datasourceName": {"type": "string", "description": "连接名称"},
        "host": {"type": "string", "description": "主机地址"},
        "port": {"type": "integer", "description": "端口"},
        "username": {"type": "string", "description": "用户名"},
        "password": {"type": "string", "description": "密码"},
        "remark": {"type": "string", "description": "更新备注"},
    },
    required=["id"],
    param_categories={
        "id": "body",
        "datasourceName": "body",
        "host": "body",
        "port": "body",
        "username": "body",
        "password": "body",
        "remark": "body",
    },
)

DELETE_CONNECTION = _tool(
    name="delete_connection",
    description="删除指定的连接实例",
    method="DELETE",
    path="/datasource/connection/{id}",
    properties={
        "id": {"type": "integer", "description": "连接实例ID"},
    },
    required=["id"],
    param_categories={"id": "path"},
)

TEST_CONNECTION = _tool(
    name="test_connection",
    description="测试数据库连接是否可用",
    method="POST",
    path="/datasource/connection/test",
    properties={
        "id": {"type": "integer", "description": "连接实例ID"},
        "host": {"type": "string", "description": "主机地址"},
        "port": {"type": "integer", "description": "端口"},
        "username": {"type": "string", "description": "用户名"},
        "password": {"type": "string", "description": "密码"},
        "datasourceType": {"type": "string", "description": "数据库类型，例如 PostgreSQL（默认 PostgreSQL）", "default": "PostgreSQL"},
    },
    required=["host", "port", "username", "password"],
    param_categories={
        "id": "body",
        "host": "body",
        "port": "body",
        "username": "body",
        "password": "body",
        "datasourceType": "body",
    },
)

CHANGE_CONNECTION_STATUS = _tool(
    name="change_connection_status",
    description="修改连接实例的启用/禁用状态",
    method="PUT",
    path="/datasource/connection/changeStatus",
    properties={
        "id": {"type": "integer", "description": "连接实例ID"},
        "status": {"type": "integer", "description": "状态（0正常/1停用）"},
    },
    required=["id", "status"],
    param_categories={"id": "body", "status": "body"},
)

REALTIME_STRUCTURE = _tool(
    name="realtime_structure",
    description="实时查询连接下的所有数据库和表结构（直接连接数据库服务器查询）",
    method="GET",
    path="/datasource/connection/realtime/structure/{id}",
    properties={
        "id": {"type": "integer", "description": "连接实例ID"},
    },
    required=["id"],
    param_categories={"id": "path"},
)

REALTIME_DATABASES = _tool(
    name="realtime_databases",
    description="实时查询连接下的所有数据库名称",
    method="GET",
    path="/datasource/connection/realtime/databases/{id}",
    properties={
        "id": {"type": "integer", "description": "连接实例ID"},
    },
    required=["id"],
    param_categories={"id": "path"},
)

REALTIME_TABLES = _tool(
    name="realtime_tables",
    description="实时查询指定数据库下的所有表",
    method="GET",
    path="/datasource/connection/realtime/tables/{id}",
    properties={
        "id": {"type": "integer", "description": "连接实例ID"},
        "databaseName": {"type": "string", "description": "数据库名称"},
    },
    required=["id", "databaseName"],
    param_categories={"id": "path", "databaseName": "query"},
)

CREATE_BUILTIN_POSTGRESQL = _tool(
    name="create_builtin_postgresql",
    description="创建内置 PostgreSQL 数据库连接（使用配置文件中的连接信息）",
    method="POST",
    path="/datasource/connection/create_builtin_postgresql",
    properties={
        "datasourceName": {"type": "string", "description": "连接名称"},
        "remark": {"type": "string", "description": "备注"},
    },
    required=["datasourceName"],
    param_categories={"datasourceName": "body", "remark": "body"},
)

UPDATE_BUILTIN_DATABASE = _tool(
    name="update_builtin_database",
    description="修改内置 PostgreSQL 连接信息",
    method="PUT",
    path="/datasource/connection/update_builtin_database",
    properties={
        "connectionId": {"type": "integer", "description": "连接实例ID"},
        "datasourceName": {"type": "string", "description": "新名称"},
        "remark": {"type": "string", "description": "更新备注"},
    },
    required=["connectionId"],
    param_categories={"connectionId": "body", "datasourceName": "body", "remark": "body"},
)

EXECUTE_SQL = _tool(
    name="execute_sql",
    description="在指定数据源上执行 SQL 语句，支持参数化查询和环境切换",
    method="POST",
    path="/datasource/connection/{datasourceId}/execute_sql",
    properties={
        "datasourceId": {"type": "integer", "description": "数据源配置ID"},
        "target": {
            "type": "string",
            "description": "目标环境（prod/test，默认 prod）",
            "default": "prod",
        },
        "sql": {"type": "string", "description": "SQL 语句，使用 ? 占位符"},
        "params": {
            "type": "array",
            "description": "SQL 参数列表",
            "items": {},
        },
        "databaseName": {"type": "string", "description": "数据库名称"},
        **_API_KEY_PROP,
    },
    required=["datasourceId", "sql"],
    param_categories={
        "datasourceId": "path",
        "target": "query",
        "sql": "body",
        "params": "body",
        "databaseName": "body",
        "apiKey": "header",
    },
)

CREATE_DATABASE = _tool(
    name="create_database",
    description="在指定连接上创建新数据库（目前仅支持 PostgreSQL）",
    method="POST",
    path="/datasource/connection/{connectionId}/create_database",
    properties={
        "connectionId": {"type": "integer", "description": "连接实例ID"},
        "databaseName": {"type": "string", "description": "数据库名称"},
        "encoding": {"type": "string", "description": "字符编码，例如 UTF8"},
        "owner": {"type": "string", "description": "所有者"},
        **_API_KEY_PROP,
    },
    required=["connectionId", "databaseName"],
    param_categories={
        "connectionId": "path",
        "databaseName": "body",
        "encoding": "body",
        "owner": "body",
        "apiKey": "header",
    },
)

CREATE_TABLE = _tool(
    name="create_table",
    description="在指定数据库中创建新表",
    method="POST",
    path="/datasource/connection/{connectionId}/create_table",
    properties={
        "connectionId": {"type": "integer", "description": "连接实例ID"},
        "databaseName": {"type": "string", "description": "数据库名称"},
        "tableName": {"type": "string", "description": "表名"},
        "tableComment": {"type": "string", "description": "表注释"},
        "columns": {
            "type": "array",
            "description": "列定义列表",
            "items": {
                "type": "object",
                "properties": {
                    "columnName": {"type": "string"},
                    "columnType": {"type": "string"},
                    "columnLength": {"type": "integer"},
                    "isPrimaryKey": {"type": "boolean"},
                    "isNullable": {"type": "boolean"},
                    "columnComment": {"type": "string"},
                },
            },
        },
        **_API_KEY_PROP,
    },
    required=["connectionId", "databaseName", "tableName", "columns"],
    param_categories={
        "connectionId": "path",
        "databaseName": "body",
        "tableName": "body",
        "tableComment": "body",
        "columns": "body",
        "apiKey": "header",
    },
)

CREATE_DATABASE_TABLE = _tool(
    name="create_database_table",
    description="同时创建数据库和表（一次性操作）",
    method="POST",
    path="/datasource/connection/{connectionId}/create_database_table",
    properties={
        "connectionId": {"type": "integer", "description": "连接实例ID"},
        "databaseName": {"type": "string", "description": "数据库名称"},
        "encoding": {"type": "string", "description": "字符编码"},
        "owner": {"type": "string", "description": "所有者"},
        "tables": {
            "type": "array",
            "description": "要创建的表列表",
            "items": {
                "type": "object",
                "properties": {
                    "tableName": {"type": "string"},
                    "tableComment": {"type": "string"},
                    "columns": {"type": "array"},
                },
            },
        },
        **_API_KEY_PROP,
    },
    required=["connectionId", "databaseName", "tables"],
    param_categories={
        "connectionId": "path",
        "databaseName": "body",
        "encoding": "body",
        "owner": "body",
        "tables": "body",
        "apiKey": "header",
    },
)

ALTER_DATABASE = _tool(
    name="alter_database",
    description="修改数据库属性（重命名、更改所有者、更改编码）",
    method="PUT",
    path="/datasource/connection/{connectionId}/alter_database",
    properties={
        "connectionId": {"type": "integer", "description": "连接实例ID"},
        "databaseName": {"type": "string", "description": "原数据库名称"},
        "newName": {"type": "string", "description": "新数据库名称"},
        "newOwner": {"type": "string", "description": "新所有者"},
        "newEncoding": {"type": "string", "description": "新字符编码"},
        **_API_KEY_PROP,
    },
    required=["connectionId", "databaseName"],
    param_categories={
        "connectionId": "path",
        "databaseName": "body",
        "newName": "body",
        "newOwner": "body",
        "newEncoding": "body",
        "apiKey": "header",
    },
)

ALTER_TABLE = _tool(
    name="alter_table",
    description="修改表结构（添加列、删除列、重命名列、修改列类型等）",
    method="PUT",
    path="/datasource/connection/{connectionId}/alter_table",
    properties={
        "connectionId": {"type": "integer", "description": "连接实例ID"},
        "databaseName": {"type": "string", "description": "数据库名称"},
        "tableName": {"type": "string", "description": "表名"},
        "tableComment": {"type": "string", "description": "表注释"},
        "operations": {
            "type": "array",
            "description": "操作列表",
            "items": {
                "type": "object",
                "properties": {
                    "operation": {"type": "string"},
                    "column": {"type": "object"},
                },
            },
        },
        **_API_KEY_PROP,
    },
    required=["connectionId", "databaseName", "tableName", "operations"],
    param_categories={
        "connectionId": "path",
        "databaseName": "body",
        "tableName": "body",
        "tableComment": "body",
        "operations": "body",
        "apiKey": "header",
    },
)

GENERATE_TABLE = _tool(
    name="generate_table",
    description="使用 AI 根据需求描述生成表结构（异步任务）",
    method="POST",
    path="/datasource/connection/generate_table",
    properties={
        "requirement": {"type": "string", "description": "需求描述"},
        "databaseId": {"type": "integer", "description": "关联的数据库ID"},
    },
    required=["requirement"],
    param_categories={"requirement": "body", "databaseId": "body"},
)

IMPORT_DOCUMENT_PREVIEW = _tool(
    name="import_document_preview",
    description="上传 Excel/CSV 文件，AI 识别表结构并预览前 10 条数据",
    method="POST",
    path="/datasource/connection/{connectionId}/import_document/preview",
    properties={
        "connectionId": {"type": "integer", "description": "连接实例ID"},
        "target": {
            "type": "string",
            "description": "目标环境（prod/test，默认 prod）",
            "default": "prod",
        },
        "filePath": {"type": "string", "description": "本地 Excel/CSV 文件路径"},
    },
    required=["connectionId", "filePath"],
    param_categories={
        "connectionId": "path",
        "target": "query",
        "filePath": "file",
    },
)

IMPORT_DOCUMENT_CONFIRM = _tool(
    name="import_document_confirm",
    description="确认导入，创建表并插入数据",
    method="POST",
    path="/datasource/connection/{connectionId}/import_document/confirm",
    properties={
        "connectionId": {"type": "integer", "description": "连接实例ID"},
        "target": {
            "type": "string",
            "description": "目标环境（prod/test，默认 prod）",
            "default": "prod",
        },
        "tableStructure": {
            "type": "object",
            "description": "表结构定义",
        },
        "allData": {
            "type": "array",
            "description": "完整导入数据",
        },
        **_API_KEY_PROP,
    },
    required=["connectionId", "tableStructure", "allData"],
    param_categories={
        "connectionId": "path",
        "target": "query",
        "tableStructure": "body",
        "allData": "body",
        "apiKey": "header",
    },
)

BUILTIN_TABLE_DATA = _tool(
    name="builtin_table_data",
    description="根据表ID查询表结构和数据（分页）",
    method="GET",
    path="/datasource/connection/builtin/table/{tableId}",
    properties={
        "tableId": {"type": "integer", "description": "表ID"},
        "pageNum": {"type": "integer", "description": "页码（默认1）"},
        "pageSize": {"type": "integer", "description": "每页数量（默认100）"},
        "target": {
            "type": "string",
            "description": "目标环境（prod/test，默认 prod）",
            "default": "prod",
        },
    },
    required=["tableId"],
    param_categories={
        "tableId": "path",
        "pageNum": "query",
        "pageSize": "query",
        "target": "query",
    },
)

BUILTIN_TABLE_INSERT = _tool(
    name="builtin_table_insert",
    description="向指定表插入一条数据",
    method="POST",
    path="/datasource/connection/builtin/table/{tableId}/rows",
    properties={
        "tableId": {"type": "integer", "description": "表ID"},
        "target": {
            "type": "string",
            "description": "目标环境（prod/test，默认 prod）",
            "default": "prod",
        },
        "data": {"type": "object", "description": "要插入的数据"},
    },
    required=["tableId", "data"],
    param_categories={
        "tableId": "path",
        "target": "query",
        "data": "body",
    },
)

BUILTIN_TABLE_UPDATE = _tool(
    name="builtin_table_update",
    description="根据主键更新表数据",
    method="PUT",
    path="/datasource/connection/builtin/table/{tableId}/rows",
    properties={
        "tableId": {"type": "integer", "description": "表ID"},
        "target": {
            "type": "string",
            "description": "目标环境（prod/test，默认 prod）",
            "default": "prod",
        },
        "data": {"type": "object", "description": "要更新的数据"},
        "primaryKey": {"type": "object", "description": "主键条件"},
    },
    required=["tableId", "data", "primaryKey"],
    param_categories={
        "tableId": "path",
        "target": "query",
        "data": "body",
        "primaryKey": "body",
    },
)

BUILTIN_TABLE_DELETE = _tool(
    name="builtin_table_delete",
    description="根据主键批量删除表数据",
    method="DELETE",
    path="/datasource/connection/builtin/table/{tableId}/rows",
    properties={
        "tableId": {"type": "integer", "description": "表ID"},
        "target": {
            "type": "string",
            "description": "目标环境（prod/test，默认 prod）",
            "default": "prod",
        },
        "primaryKeys": {
            "type": "array",
            "description": "主键条件列表",
            "items": {"type": "object"},
        },
    },
    required=["tableId", "primaryKeys"],
    param_categories={
        "tableId": "path",
        "target": "query",
        "primaryKeys": "body",
    },
)

ALL_TOOLS = [
    LIST_DATASOURCE_CONFIGS,
    GET_DATASOURCE_CONFIG,
    BATCH_CREATE_DATASOURCE_CONFIGS,
    REPLACE_DATASOURCE_CONFIGS,
    BATCH_UPDATE_DATASOURCE_CONFIGS,
    DELETE_DATASOURCE_CONFIGS,
    TEST_CONNECTION_CONFIG,
    CHANGE_DATASOURCE_STATUS,
    EXPORT_DATASOURCE_CONFIGS,
    LIST_CONNECTIONS,
    GET_CONNECTION,
    CREATE_CONNECTION,
    UPDATE_CONNECTION,
    DELETE_CONNECTION,
    TEST_CONNECTION,
    CHANGE_CONNECTION_STATUS,
    REALTIME_STRUCTURE,
    REALTIME_DATABASES,
    REALTIME_TABLES,
    CREATE_BUILTIN_POSTGRESQL,
    UPDATE_BUILTIN_DATABASE,
    EXECUTE_SQL,
    CREATE_DATABASE,
    CREATE_TABLE,
    CREATE_DATABASE_TABLE,
    ALTER_DATABASE,
    ALTER_TABLE,
    GENERATE_TABLE,
    IMPORT_DOCUMENT_PREVIEW,
    IMPORT_DOCUMENT_CONFIRM,
    BUILTIN_TABLE_DATA,
    BUILTIN_TABLE_INSERT,
    BUILTIN_TABLE_UPDATE,
    BUILTIN_TABLE_DELETE,
]


TOOL_MAP = {tool["name"]: tool for tool in ALL_TOOLS}


def list_tools() -> List[Dict[str, Any]]:
    """Return all tool definitions."""
    return ALL_TOOLS


def get_tool(name: str) -> Dict[str, Any]:
    """Return a tool definition by name."""
    return TOOL_MAP.get(name)
