-- AI MCP挂载完整初始化脚本
-- 包含：
-- 1. 创建 sys_ai_mcp_mount 表
-- 2. 初始化默认本地挂载 wcs_local
--
-- 说明：
-- 1. 本脚本按当前代码版本生成，挂载配置使用单字段 url，不再拆分 base_url / endpoint。
-- 2. 默认挂载地址使用本地开发环境：http://127.0.0.1:9090/wcs
--    如果你的部署地址不同，执行后可在“MCP挂载”页面修改。
-- 3. 菜单初始化已拆分到独立脚本：20260312_init_ai_management_menu.sql

CREATE TABLE IF NOT EXISTS `sys_ai_mcp_mount` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(128) NOT NULL COMMENT '挂载名称',
  `mount_code` varchar(64) NOT NULL COMMENT '挂载编码',
  `transport_type` varchar(32) NOT NULL COMMENT '传输类型：SSE/STREAMABLE_HTTP',
  `url` varchar(500) NOT NULL COMMENT 'MCP完整URL',
  `request_timeout_ms` int NOT NULL DEFAULT '20000' COMMENT '请求超时毫秒',
  `priority` int NOT NULL DEFAULT '100' COMMENT '优先级，越小越优先',
  `status` tinyint NOT NULL DEFAULT '1' COMMENT '状态：1启用 0禁用',
  `last_test_ok` tinyint DEFAULT NULL COMMENT '最近一次测试是否成功：1成功 0失败',
  `last_test_time` datetime DEFAULT NULL COMMENT '最近测试时间',
  `last_test_summary` varchar(1000) DEFAULT NULL COMMENT '最近测试摘要',
  `memo` varchar(1000) DEFAULT NULL COMMENT '备注',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_sys_ai_mcp_mount_code` (`mount_code`),
  KEY `idx_sys_ai_mcp_mount_status_priority` (`status`,`priority`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI MCP挂载配置表';

INSERT INTO `sys_ai_mcp_mount`
(`name`, `mount_code`, `transport_type`, `url`, `request_timeout_ms`, `priority`, `status`, `memo`)
SELECT
  'WCS默认MCP',
  'wcs_local',
  'SSE',
  'http://127.0.0.1:9090/wcs/ai/mcp/sse',
  20000,
  0,
  1,
  '默认挂载当前WCS自身的MCP服务，AI助手也通过挂载配置访问本系统工具'
FROM dual
WHERE NOT EXISTS (
  SELECT 1
  FROM `sys_ai_mcp_mount`
  WHERE `mount_code` = 'wcs_local'
);

UPDATE `sys_ai_mcp_mount`
SET
  `name` = CASE WHEN `name` IS NULL OR TRIM(`name`) = '' THEN 'WCS默认MCP' ELSE `name` END,
  `transport_type` = CASE WHEN `transport_type` IS NULL OR TRIM(`transport_type`) = '' THEN 'SSE' ELSE `transport_type` END,
  `url` = CASE
    WHEN `url` IS NULL OR TRIM(`url`) = '' THEN 'http://127.0.0.1:9090/wcs/ai/mcp/sse'
    WHEN TRIM(`url`) = 'http://127.0.0.1:9090' THEN 'http://127.0.0.1:9090/wcs/ai/mcp/sse'
    WHEN TRIM(`url`) = 'http://localhost:9090' THEN 'http://127.0.0.1:9090/wcs/ai/mcp/sse'
    WHEN TRIM(`url`) = 'http://127.0.0.1:9090/wcs' THEN 'http://127.0.0.1:9090/wcs/ai/mcp/sse'
    ELSE `url`
  END,
  `request_timeout_ms` = CASE WHEN `request_timeout_ms` IS NULL OR `request_timeout_ms` < 1000 THEN 20000 ELSE `request_timeout_ms` END,
  `priority` = CASE WHEN `priority` IS NULL THEN 0 ELSE `priority` END,
  `status` = CASE WHEN `status` IS NULL THEN 1 ELSE `status` END,
  `memo` = CASE WHEN `memo` IS NULL OR TRIM(`memo`) = '' THEN '默认挂载当前WCS自身的MCP服务，AI助手也通过挂载配置访问本系统工具' ELSE `memo` END
WHERE `mount_code` = 'wcs_local';

SELECT id, name, mount_code, transport_type, url, priority, status
FROM sys_ai_mcp_mount
WHERE mount_code = 'wcs_local';
