START TRANSACTION;

SET @meeting_manage_id := (SELECT menu_id FROM sys_menus WHERE menu_code = 'meeting_manage' LIMIT 1);
SET @history_meetings_id := (SELECT menu_id FROM sys_menus WHERE menu_code = 'history_meetings' LIMIT 1);
SET @prompt_config_id := (SELECT menu_id FROM sys_menus WHERE menu_code = 'prompt_config' LIMIT 1);
SET @personal_prompt_library_id := (SELECT menu_id FROM sys_menus WHERE menu_code = 'personal_prompt_library' LIMIT 1);

INSERT INTO sys_menus (
  menu_code, menu_name, menu_icon, menu_url, menu_type, parent_id, sort_order, is_active, description
)
SELECT
  'history_meetings', '历史会议', 'CalendarOutlined', '/meetings/history', 'link', @meeting_manage_id, 1, 1, '普通用户历史会议'
FROM DUAL
WHERE @meeting_manage_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM sys_menus WHERE menu_code = 'history_meetings');

SET @history_meetings_id := (SELECT menu_id FROM sys_menus WHERE menu_code = 'history_meetings' LIMIT 1);

UPDATE sys_menus
SET
  parent_id = @meeting_manage_id,
  sort_order = 2,
  menu_name = '提示词配置',
  menu_icon = COALESCE(menu_icon, 'BookOutlined')
WHERE menu_code = 'prompt_config';

INSERT INTO sys_menus (
  menu_code, menu_name, menu_icon, menu_url, menu_type, parent_id, sort_order, is_active, description
)
SELECT
  'personal_prompt_library', '个人提示词仓库', 'ReadOutlined', '/personal-prompts', 'link', @meeting_manage_id, 3, 1, '普通用户个人提示词仓库'
FROM DUAL
WHERE @meeting_manage_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM sys_menus WHERE menu_code = 'personal_prompt_library');

SET @personal_prompt_library_id := (SELECT menu_id FROM sys_menus WHERE menu_code = 'personal_prompt_library' LIMIT 1);

INSERT IGNORE INTO sys_role_menu_permissions (role_id, menu_id, granted_at)
SELECT 2, @meeting_manage_id, NOW()
FROM DUAL
WHERE @meeting_manage_id IS NOT NULL;

INSERT IGNORE INTO sys_role_menu_permissions (role_id, menu_id, granted_at)
SELECT 2, @history_meetings_id, NOW()
FROM DUAL
WHERE @history_meetings_id IS NOT NULL;

INSERT IGNORE INTO sys_role_menu_permissions (role_id, menu_id, granted_at)
SELECT 2, @prompt_config_id, NOW()
FROM DUAL
WHERE @prompt_config_id IS NOT NULL;

INSERT IGNORE INTO sys_role_menu_permissions (role_id, menu_id, granted_at)
SELECT 2, @personal_prompt_library_id, NOW()
FROM DUAL
WHERE @personal_prompt_library_id IS NOT NULL;

COMMIT;
