-- Nex Basse init schema (MySQL 5.7/8.0)
-- Charset: utf8mb4, Collation: utf8mb4_unicode_ci

CREATE TABLE IF NOT EXISTS sys_user (
  user_id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(50) NOT NULL UNIQUE,
  display_name VARCHAR(50) NOT NULL,
  email VARCHAR(100) UNIQUE,
  phone VARCHAR(30) UNIQUE,
  password_hash VARCHAR(255) NOT NULL,
  status TINYINT(1) NOT NULL DEFAULT 1,
  is_deleted TINYINT(1) NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_role (
  role_id INT AUTO_INCREMENT PRIMARY KEY,
  role_code VARCHAR(50) NOT NULL UNIQUE,
  role_name VARCHAR(50) NOT NULL,
  status TINYINT(1) NOT NULL DEFAULT 1,
  remark TEXT,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_user_role (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT NOT NULL,
  role_id INT NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_user_role (user_id, role_id),
  INDEX idx_user_role_user (user_id),
  INDEX idx_user_role_role (role_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_permission (
  perm_id INT AUTO_INCREMENT PRIMARY KEY,
  parent_id INT NULL,
  name VARCHAR(100) NOT NULL,
  code VARCHAR(100) NOT NULL UNIQUE,
  perm_type VARCHAR(20) NOT NULL,
  level INT NOT NULL,
  path VARCHAR(255),
  component VARCHAR(255),
  icon VARCHAR(100),
  sort_order INT NOT NULL DEFAULT 0,
  is_visible TINYINT(1) NOT NULL DEFAULT 1,
  status TINYINT(1) NOT NULL DEFAULT 1,
  description TEXT,
  meta JSON,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_perm_parent (parent_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_role_permission (
  id INT AUTO_INCREMENT PRIMARY KEY,
  role_id INT NOT NULL,
  perm_id INT NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_role_perm (role_id, perm_id),
  INDEX idx_role_perm_role (role_id),
  INDEX idx_role_perm_perm (perm_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_dict_type (
  dict_type_id INT AUTO_INCREMENT PRIMARY KEY,
  type_code VARCHAR(50) NOT NULL UNIQUE,
  type_name VARCHAR(50) NOT NULL,
  status TINYINT(1) NOT NULL DEFAULT 1,
  remark TEXT,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_dict_item (
  dict_item_id INT AUTO_INCREMENT PRIMARY KEY,
  type_code VARCHAR(50) NOT NULL,
  item_label VARCHAR(100) NOT NULL,
  item_value VARCHAR(100) NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  status TINYINT(1) NOT NULL DEFAULT 1,
  remark TEXT,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_dict_item (type_code, item_value),
  INDEX idx_dict_item_type (type_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_param (
  param_id INT AUTO_INCREMENT PRIMARY KEY,
  param_key VARCHAR(100) NOT NULL UNIQUE,
  param_value TEXT NOT NULL,
  param_type VARCHAR(20) NOT NULL DEFAULT 'string',
  status TINYINT(1) NOT NULL DEFAULT 1,
  is_system TINYINT(1) NOT NULL DEFAULT 0,
  description TEXT,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Default system params
INSERT INTO sys_param (param_key, param_value, param_type, status, is_system, description)
VALUES
  ('security.access_token_minutes', '60', 'int', 1, 1, 'Access token 有效期(分钟)'),
  ('security.refresh_token_minutes', '10080', 'int', 1, 1, 'Refresh token 有效期(分钟)')
ON DUPLICATE KEY UPDATE param_value=VALUES(param_value);

-- Default admin role
INSERT IGNORE INTO sys_role (role_code, role_name, status, remark)
VALUES ('admin', '管理员', 1, '系统默认管理员');

-- Seed permissions (3-level menu/button)
INSERT IGNORE INTO sys_permission (perm_id, parent_id, name, code, perm_type, level, path, component, icon, sort_order, is_visible, status, description, meta)
VALUES
  (1, NULL, 'AI 战略洞察', 'strategy.ai', 'menu', 1, '/strategy', NULL, 'strategy', 1, 1, 1, '一级菜单', NULL),
  (2, NULL, '工作空间', 'workspace', 'menu', 1, '/workspace', NULL, 'workspace', 2, 1, 1, '一级菜单', NULL),
  (3, NULL, '知识库', 'ai_agent', 'menu', 1, '/ai-agent', NULL, 'ai', 3, 1, 1, '一级菜单', NULL),
  (4, NULL, '系统管理', 'system', 'menu', 1, '/system', NULL, 'system', 4, 1, 1, '一级菜单', NULL),

  (10, 1, 'AI 战略洞察', 'strategy.ai.home', 'menu', 2, '/strategy/insights', 'StrategyHome', 'insight', 1, 1, 1, '二级菜单', NULL),
  (11, 2, '实时会议', 'workspace.realtime', 'menu', 2, '/workspace/meeting', 'MeetingLive', 'meeting', 1, 1, 1, '二级菜单', NULL),
  (12, 2, '历史记录', 'workspace.history', 'menu', 2, '/workspace/history', 'MeetingHistory', 'history', 2, 1, 1, '二级菜单', NULL),
  (13, 2, '声纹档案', 'workspace.voice', 'menu', 2, '/workspace/voice', 'VoiceProfile', 'voice', 3, 1, 1, '二级菜单', NULL),
  (14, 3, '知识库管理', 'ai_agent.kb', 'menu', 2, '/ai-agent/kb', 'KnowledgeBase', 'kb', 1, 1, 1, '二级菜单', NULL),
  (15, 3, '总结模板管理', 'ai_agent.templates', 'menu', 2, '/ai-agent/templates', 'PromptTemplates', 'template', 2, 1, 1, '二级菜单', NULL),
  (16, 3, '热词管理', 'ai_agent.hotwords', 'menu', 2, '/ai-agent/hotwords', 'Hotwords', 'hot', 3, 1, 1, '二级菜单', NULL),
  (17, 4, '系统设置', 'system.setting', 'menu', 2, '/system/settings', 'SystemSettings', 'setting', 1, 1, 1, '二级菜单', NULL),
  (18, 4, '用户管理', 'system.users', 'menu', 2, '/system/users', 'UserManage', 'user', 2, 1, 1, '二级菜单', NULL),
  (19, 4, '角色管理', 'system.roles', 'menu', 2, '/system/roles', 'RoleManage', 'role', 3, 1, 1, '二级菜单', NULL),
  (20, 4, '系统指标', 'system.metrics', 'menu', 2, '/system/metrics', 'SystemMetrics', 'metrics', 4, 1, 1, '二级菜单', NULL),

  (101, 18, '创建用户', 'system.users.create', 'button', 3, NULL, NULL, NULL, 1, 1, 1, '按钮权限', NULL),
  (102, 18, '编辑用户', 'system.users.edit', 'button', 3, NULL, NULL, NULL, 2, 1, 1, '按钮权限', NULL),
  (103, 18, '删除用户', 'system.users.delete', 'button', 3, NULL, NULL, NULL, 3, 1, 1, '按钮权限', NULL),
  (111, 19, '创建角色', 'system.roles.create', 'button', 3, NULL, NULL, NULL, 1, 1, 1, '按钮权限', NULL),
  (112, 19, '编辑角色', 'system.roles.edit', 'button', 3, NULL, NULL, NULL, 2, 1, 1, '按钮权限', NULL),
  (113, 19, '分配权限', 'system.roles.assign', 'button', 3, NULL, NULL, NULL, 3, 1, 1, '按钮权限', NULL);

-- Grant all permissions to admin role
INSERT IGNORE INTO sys_role_permission (role_id, perm_id)
SELECT r.role_id, p.perm_id
FROM sys_role r
JOIN sys_permission p
WHERE r.role_code = 'admin';
