-- AI 管理菜单统一初始化脚本
-- 包含：
-- 1. 创建一级菜单：AI管理
-- 2. 将 AI配置 / Prompt配置 / MCP挂载 统一挂到 AI管理 下
-- 3. 补齐三个菜单各自的“查看”权限
--
-- 说明：
-- 1. 如果这三个菜单此前已经挂在“开发专用”下，本脚本会直接迁移，不会重复插入。
-- 2. 执行后请在“角色授权”里给对应角色勾选 AI管理 下的新菜单与查看权限。

SET @ai_manage_id := COALESCE(
  (
    SELECT id
    FROM sys_resource
    WHERE code = 'aiManage' AND level = 1
    ORDER BY id
    LIMIT 1
  ),
  (
    SELECT id
    FROM sys_resource
    WHERE name = 'AI管理' AND level = 1
    ORDER BY id
    LIMIT 1
  )
);

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'aiManage', 'AI管理', NULL, 1, 100, 1
FROM dual
WHERE @ai_manage_id IS NULL;

SET @ai_manage_id := COALESCE(
  (
    SELECT id
    FROM sys_resource
    WHERE code = 'aiManage' AND level = 1
    ORDER BY id
    LIMIT 1
  ),
  (
    SELECT id
    FROM sys_resource
    WHERE name = 'AI管理' AND level = 1
    ORDER BY id
    LIMIT 1
  )
);

UPDATE sys_resource
SET code = 'aiManage',
    name = 'AI管理',
    resource_id = NULL,
    level = 1,
    sort = 100,
    status = 1
WHERE id = @ai_manage_id;

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'ai/llm_config.html', 'AI配置', @ai_manage_id, 2, 1, 1
FROM dual
WHERE @ai_manage_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'ai/llm_config.html' AND level = 2
  );

UPDATE sys_resource
SET name = 'AI配置',
    resource_id = @ai_manage_id,
    level = 2,
    sort = 1,
    status = 1
WHERE code = 'ai/llm_config.html' AND level = 2;

SET @ai_cfg_id := (
  SELECT id
  FROM sys_resource
  WHERE code = 'ai/llm_config.html' AND level = 2
  ORDER BY id
  LIMIT 1
);

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'ai/llm_config.html#view', '查看', @ai_cfg_id, 3, 1, 1
FROM dual
WHERE @ai_cfg_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'ai/llm_config.html#view' AND level = 3
  );

UPDATE sys_resource
SET name = '查看',
    resource_id = @ai_cfg_id,
    level = 3,
    sort = 1,
    status = 1
WHERE code = 'ai/llm_config.html#view' AND level = 3;

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'ai/prompt_config.html', 'Prompt配置', @ai_manage_id, 2, 2, 1
FROM dual
WHERE @ai_manage_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'ai/prompt_config.html' AND level = 2
  );

UPDATE sys_resource
SET name = 'Prompt配置',
    resource_id = @ai_manage_id,
    level = 2,
    sort = 2,
    status = 1
WHERE code = 'ai/prompt_config.html' AND level = 2;

SET @ai_prompt_id := (
  SELECT id
  FROM sys_resource
  WHERE code = 'ai/prompt_config.html' AND level = 2
  ORDER BY id
  LIMIT 1
);

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'ai/prompt_config.html#view', '查看', @ai_prompt_id, 3, 1, 1
FROM dual
WHERE @ai_prompt_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'ai/prompt_config.html#view' AND level = 3
  );

UPDATE sys_resource
SET name = '查看',
    resource_id = @ai_prompt_id,
    level = 3,
    sort = 1,
    status = 1
WHERE code = 'ai/prompt_config.html#view' AND level = 3;

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'ai/mcp_mount.html', 'MCP挂载', @ai_manage_id, 2, 3, 1
FROM dual
WHERE @ai_manage_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'ai/mcp_mount.html' AND level = 2
  );

UPDATE sys_resource
SET name = 'MCP挂载',
    resource_id = @ai_manage_id,
    level = 2,
    sort = 3,
    status = 1
WHERE code = 'ai/mcp_mount.html' AND level = 2;

SET @ai_mcp_mount_id := (
  SELECT id
  FROM sys_resource
  WHERE code = 'ai/mcp_mount.html' AND level = 2
  ORDER BY id
  LIMIT 1
);

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'ai/mcp_mount.html#view', '查看', @ai_mcp_mount_id, 3, 1, 1
FROM dual
WHERE @ai_mcp_mount_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'ai/mcp_mount.html#view' AND level = 3
  );

UPDATE sys_resource
SET name = '查看',
    resource_id = @ai_mcp_mount_id,
    level = 3,
    sort = 1,
    status = 1
WHERE code = 'ai/mcp_mount.html#view' AND level = 3;

SELECT id, code, name, resource_id, level, sort, status
FROM sys_resource
WHERE code IN (
  'aiManage',
  'ai/llm_config.html',
  'ai/llm_config.html#view',
  'ai/prompt_config.html',
  'ai/prompt_config.html#view',
  'ai/mcp_mount.html',
  'ai/mcp_mount.html#view'
)
ORDER BY level, sort, id;
