-- Migration: prompt library upgrade + user prompt config + menu regroup
-- Created at: 2026-03-13

BEGIN;

-- 1) prompts table: support system prompt library
SET @add_is_system_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'prompts' AND column_name = 'is_system'
    ),
    'SELECT 1',
    'ALTER TABLE prompts ADD COLUMN is_system TINYINT(1) NOT NULL DEFAULT 0 AFTER creator_id'
  )
);
PREPARE stmt FROM @add_is_system_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @add_prompts_idx_sql = (
  SELECT IF(
    EXISTS (
      SELECT 1
      FROM information_schema.statistics
      WHERE table_schema = DATABASE()
        AND table_name = 'prompts'
        AND index_name = 'idx_prompts_task_scope_active'
    ),
    'SELECT 1',
    'CREATE INDEX idx_prompts_task_scope_active ON prompts (task_type, is_system, creator_id, is_active, is_default)'
  )
);
PREPARE stmt FROM @add_prompts_idx_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Existing admin-created prompts become system prompts by default
UPDATE prompts
SET is_system = 1
WHERE creator_id = 1;

-- 2) user prompt config table
CREATE TABLE IF NOT EXISTS prompt_config (
  config_id BIGINT(20) NOT NULL AUTO_INCREMENT,
  user_id INT(11) NOT NULL,
  task_type ENUM('MEETING_TASK','KNOWLEDGE_TASK') NOT NULL,
  prompt_id INT(11) NOT NULL,
  is_enabled TINYINT(1) NOT NULL DEFAULT 1,
  sort_order INT(11) NOT NULL DEFAULT 0,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (config_id),
  UNIQUE KEY uk_user_task_prompt (user_id, task_type, prompt_id),
  KEY idx_user_task_order (user_id, task_type, sort_order),
  KEY idx_prompt_id (prompt_id),
  CONSTRAINT fk_upc_user FOREIGN KEY (user_id) REFERENCES sys_users(user_id) ON DELETE CASCADE,
  CONSTRAINT fk_upc_prompt FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 3) Menu regroup:
-- move prompt_management under platform_admin (2nd level)
UPDATE sys_menus child
JOIN sys_menus parent ON parent.menu_code = 'platform_admin'
SET child.parent_id = parent.menu_id,
    child.sort_order = 8,
    child.menu_level = 2,
    child.tree_path = CONCAT(parent.tree_path, '/', child.menu_id)
WHERE child.menu_code = 'prompt_management';

-- add prompt_config entry
INSERT INTO sys_menus
(
  menu_code, menu_name, menu_icon, menu_url, menu_type,
  parent_id, menu_level, tree_path, sort_order, is_active, is_visible, description
)
SELECT
  'prompt_config',
  '提示词配置',
  'Book',
  '/prompt-config',
  'link',
  p.menu_id,
  2,
  NULL,
  9,
  1,
  1,
  '用户可配置启用提示词与排序'
FROM sys_menus p
WHERE p.menu_code = 'platform_admin'
  AND NOT EXISTS (SELECT 1 FROM sys_menus WHERE menu_code = 'prompt_config');

UPDATE sys_menus c
JOIN sys_menus p ON c.parent_id = p.menu_id
SET c.menu_level = p.menu_level + 1,
    c.tree_path = CONCAT(p.tree_path, '/', c.menu_id)
WHERE c.menu_code = 'prompt_config';

-- Keep existing role-menu permissions unchanged.
-- This migration only creates/adjusts menu definitions.

COMMIT;
