-- NEX Docus 数据库初始化脚本
-- 创建数据库
CREATE DATABASE IF NOT EXISTS `nex_docus` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `nex_docus`;

-- 1. 用户表
CREATE TABLE IF NOT EXISTS `users` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '用户ID',
  `username` VARCHAR(50) NOT NULL UNIQUE COMMENT '用户名（登录账号）',
  `password_hash` VARCHAR(255) NOT NULL COMMENT '密码哈希（bcrypt）',
  `nickname` VARCHAR(50) DEFAULT NULL COMMENT '昵称（显示名称）',
  `email` VARCHAR(100) DEFAULT NULL COMMENT '邮箱',
  `phone` VARCHAR(20) DEFAULT NULL COMMENT '手机号',
  `avatar` VARCHAR(255) DEFAULT NULL COMMENT '头像URL',
  `status` TINYINT DEFAULT 1 COMMENT '状态：0-禁用 1-启用',
  `is_superuser` TINYINT DEFAULT 0 COMMENT '是否超级管理员：0-否 1-是',
  `last_login_at` DATETIME DEFAULT NULL COMMENT '最后登录时间',
  `last_login_ip` VARCHAR(50) DEFAULT NULL COMMENT '最后登录IP',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  INDEX `idx_username` (`username`),
  INDEX `idx_email` (`email`),
  INDEX `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- 2. 角色表
CREATE TABLE IF NOT EXISTS `roles` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '角色ID',
  `role_name` VARCHAR(50) NOT NULL UNIQUE COMMENT '角色名称',
  `role_code` VARCHAR(50) NOT NULL UNIQUE COMMENT '角色编码',
  `description` VARCHAR(255) DEFAULT NULL COMMENT '角色描述',
  `status` TINYINT DEFAULT 1 COMMENT '状态：0-禁用 1-启用',
  `is_system` TINYINT DEFAULT 0 COMMENT '是否系统角色：0-否 1-是',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  INDEX `idx_role_code` (`role_code`),
  INDEX `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色表';

-- 3. 用户角色关联表
CREATE TABLE IF NOT EXISTS `user_roles` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '关联ID',
  `user_id` BIGINT NOT NULL COMMENT '用户ID',
  `role_id` BIGINT NOT NULL COMMENT '角色ID',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  UNIQUE KEY `uk_user_role` (`user_id`, `role_id`),
  INDEX `idx_user_id` (`user_id`),
  INDEX `idx_role_id` (`role_id`),
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户角色关联表';

-- 4. 系统菜单表
CREATE TABLE IF NOT EXISTS `system_menus` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '菜单ID',
  `parent_id` BIGINT DEFAULT 0 COMMENT '父菜单ID',
  `menu_name` VARCHAR(50) NOT NULL COMMENT '菜单名称',
  `menu_code` VARCHAR(50) NOT NULL UNIQUE COMMENT '菜单编码',
  `menu_type` TINYINT NOT NULL COMMENT '菜单类型：1-目录 2-菜单 3-按钮',
  `path` VARCHAR(255) DEFAULT NULL COMMENT '路由路径',
  `component` VARCHAR(255) DEFAULT NULL COMMENT '组件路径',
  `icon` VARCHAR(100) DEFAULT NULL COMMENT '图标',
  `sort_order` INT DEFAULT 0 COMMENT '排序号',
  `visible` TINYINT DEFAULT 1 COMMENT '是否可见：0-隐藏 1-显示',
  `status` TINYINT DEFAULT 1 COMMENT '状态：0-禁用 1-启用',
  `permission` VARCHAR(100) DEFAULT NULL COMMENT '权限字符串',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  INDEX `idx_parent_id` (`parent_id`),
  INDEX `idx_menu_code` (`menu_code`),
  INDEX `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统菜单表';

-- 5. 角色菜单授权表
CREATE TABLE IF NOT EXISTS `role_menus` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '关联ID',
  `role_id` BIGINT NOT NULL COMMENT '角色ID',
  `menu_id` BIGINT NOT NULL COMMENT '菜单ID',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  UNIQUE KEY `uk_role_menu` (`role_id`, `menu_id`),
  INDEX `idx_role_id` (`role_id`),
  INDEX `idx_menu_id` (`menu_id`),
  FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`menu_id`) REFERENCES `system_menus`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色菜单授权表';

-- 6. 项目表
CREATE TABLE IF NOT EXISTS `projects` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '项目ID',
  `name` VARCHAR(100) NOT NULL COMMENT '项目名称',
  `description` VARCHAR(500) DEFAULT NULL COMMENT '项目描述',
  `storage_key` CHAR(36) NOT NULL COMMENT '磁盘存储UUID',
  `owner_id` BIGINT NOT NULL COMMENT '项目所有者ID',
  `is_public` TINYINT DEFAULT 0 COMMENT '是否公开：0-私有 1-公开',
  `is_template` TINYINT DEFAULT 0 COMMENT '是否模板项目：0-否 1-是',
  `status` TINYINT DEFAULT 1 COMMENT '状态：0-归档 1-活跃',
  `cover_image` VARCHAR(255) DEFAULT NULL COMMENT '封面图',
  `sort_order` INT DEFAULT 0 COMMENT '排序号',
  `visit_count` INT DEFAULT 0 COMMENT '访问次数',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  UNIQUE KEY `uk_storage_key` (`storage_key`),
  INDEX `idx_owner_id` (`owner_id`),
  INDEX `idx_name` (`name`),
  INDEX `idx_status` (`status`),
  INDEX `idx_created_at` (`created_at`),
  FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='项目表';

-- 7. 项目成员表
CREATE TABLE IF NOT EXISTS `project_members` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '成员ID',
  `project_id` BIGINT NOT NULL COMMENT '项目ID',
  `user_id` BIGINT NOT NULL COMMENT '用户ID',
  `role` ENUM('admin', 'editor', 'viewer') DEFAULT 'viewer' COMMENT '项目角色',
  `invited_by` BIGINT DEFAULT NULL COMMENT '邀请人ID',
  `joined_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '加入时间',
  UNIQUE KEY `uk_project_user` (`project_id`, `user_id`),
  INDEX `idx_project_id` (`project_id`),
  INDEX `idx_user_id` (`user_id`),
  INDEX `idx_role` (`role`),
  FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`invited_by`) REFERENCES `users`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='项目成员表';

-- 8. 文档元数据表
CREATE TABLE IF NOT EXISTS `document_meta` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '元数据ID',
  `project_id` BIGINT NOT NULL COMMENT '项目ID',
  `file_path` VARCHAR(500) NOT NULL COMMENT '文件相对路径',
  `title` VARCHAR(200) DEFAULT NULL COMMENT '文档标题',
  `tags` VARCHAR(500) DEFAULT NULL COMMENT '标签（JSON数组）',
  `author_id` BIGINT DEFAULT NULL COMMENT '作者ID',
  `word_count` INT DEFAULT 0 COMMENT '字数统计',
  `view_count` INT DEFAULT 0 COMMENT '浏览次数',
  `last_editor_id` BIGINT DEFAULT NULL COMMENT '最后编辑者ID',
  `last_edited_at` DATETIME DEFAULT NULL COMMENT '最后编辑时间',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  UNIQUE KEY `uk_project_path` (`project_id`, `file_path`(255)),
  INDEX `idx_project_id` (`project_id`),
  INDEX `idx_author_id` (`author_id`),
  FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`author_id`) REFERENCES `users`(`id`) ON DELETE SET NULL,
  FOREIGN KEY (`last_editor_id`) REFERENCES `users`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文档元数据表';

-- 9. 操作日志表
CREATE TABLE IF NOT EXISTS `operation_logs` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '日志ID',
  `user_id` BIGINT DEFAULT NULL COMMENT '操作用户ID',
  `username` VARCHAR(50) DEFAULT NULL COMMENT '用户名',
  `operation_type` VARCHAR(50) NOT NULL COMMENT '操作类型',
  `resource_type` VARCHAR(50) NOT NULL COMMENT '资源类型',
  `resource_id` BIGINT DEFAULT NULL COMMENT '资源ID',
  `detail` TEXT DEFAULT NULL COMMENT '操作详情（JSON）',
  `ip_address` VARCHAR(50) DEFAULT NULL COMMENT 'IP地址',
  `user_agent` VARCHAR(500) DEFAULT NULL COMMENT '用户代理',
  `status` TINYINT DEFAULT 1 COMMENT '状态：0-失败 1-成功',
  `error_message` TEXT DEFAULT NULL COMMENT '错误信息',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间',
  INDEX `idx_user_id` (`user_id`),
  INDEX `idx_resource` (`resource_type`, `resource_id`),
  INDEX `idx_created_at` (`created_at`),
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='操作日志表';

-- 10. MCP Bot 凭证表
CREATE TABLE IF NOT EXISTS `mcp_bots` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT 'Bot credential ID',
  `user_id` BIGINT NOT NULL COMMENT 'Owner user ID',
  `bot_id` VARCHAR(64) NOT NULL COMMENT 'External MCP bot id',
  `bot_secret` VARCHAR(255) NOT NULL COMMENT 'External MCP bot secret',
  `status` TINYINT DEFAULT 1 COMMENT 'Status: 0-disabled 1-enabled',
  `last_used_at` DATETIME DEFAULT NULL COMMENT 'Last successful MCP access time',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Created at',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Updated at',
  UNIQUE KEY `uk_mcp_bots_user_id` (`user_id`),
  UNIQUE KEY `uk_mcp_bots_bot_id` (`bot_id`),
  INDEX `idx_mcp_bots_status` (`status`),
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='MCP bot credentials';

-- 11. 分享链接表
CREATE TABLE IF NOT EXISTS `share_links` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '分享ID',
  `project_id` BIGINT NOT NULL COMMENT '项目ID',
  `share_type` VARCHAR(20) NOT NULL COMMENT '分享类型: project/file',
  `share_code` VARCHAR(64) NOT NULL COMMENT '公开分享码',
  `file_path` VARCHAR(500) DEFAULT NULL COMMENT '文件路径，仅文件分享使用',
  `access_pass` VARCHAR(100) DEFAULT NULL COMMENT '访问密码',
  `created_by` BIGINT DEFAULT NULL COMMENT '创建人ID',
  `status` TINYINT DEFAULT 1 COMMENT '状态：0-禁用 1-启用',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  UNIQUE KEY `uk_share_code` (`share_code`),
  INDEX `idx_share_project` (`project_id`, `share_type`, `status`),
  INDEX `idx_share_file` (`project_id`, `file_path`(255), `status`),
  INDEX `idx_share_created_by` (`created_by`),
  FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`created_by`) REFERENCES `users`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='分享链接表';

-- 12. LLM 模型配置表
CREATE TABLE IF NOT EXISTS `llm_model_config` (
  `config_id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '配置ID',
  `model_code` VARCHAR(128) NOT NULL COMMENT '模型编码',
  `model_name` VARCHAR(255) NOT NULL COMMENT '模型名称',
  `model_type` VARCHAR(32) NOT NULL DEFAULT 'chat' COMMENT '模型类型: chat/embedding',
  `provider` VARCHAR(64) DEFAULT NULL COMMENT '模型提供方',
  `endpoint_url` VARCHAR(512) DEFAULT NULL COMMENT '接口地址',
  `api_key` VARCHAR(512) DEFAULT NULL COMMENT 'API Key',
  `llm_model_name` VARCHAR(128) NOT NULL COMMENT '模型名称/部署名',
  `llm_timeout` INT NOT NULL DEFAULT 120 COMMENT '超时时间（秒）',
  `type_config` JSON NOT NULL COMMENT '模型类型差异参数',
  `description` VARCHAR(500) DEFAULT NULL COMMENT '描述',
  `is_active` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  `is_default` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否默认',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  UNIQUE KEY `uk_model_code` (`model_code`),
  INDEX `idx_model_code` (`model_code`),
  INDEX `idx_model_type` (`model_type`),
  INDEX `idx_is_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='LLM 模型配置表';

-- 13. 文档向量表
CREATE TABLE IF NOT EXISTS `document_vector` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '向量ID',
  `project_id` BIGINT NOT NULL COMMENT '项目ID',
  `file_path` VARCHAR(500) NOT NULL COMMENT '文件相对路径',
  `content_hash` VARCHAR(64) DEFAULT NULL COMMENT '内容哈希值，用于判断文件是否变更',
  `zvec_id` VARCHAR(256) DEFAULT NULL COMMENT 'ZVec返回的向量ID',
  `zvec_response` TEXT DEFAULT NULL COMMENT 'ZVec完整响应JSON',
  `status` VARCHAR(32) NOT NULL DEFAULT 'success' COMMENT '向量化状态：success/failed/pending',
  `error_message` VARCHAR(500) DEFAULT NULL COMMENT '错误信息',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  INDEX `idx_project_file` (`project_id`, `file_path`),
  FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文档向量表';

-- 14. 知识库对话会话表
CREATE TABLE IF NOT EXISTS `chat_session` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '会话ID',
  `project_id` BIGINT NOT NULL COMMENT '项目ID',
  `user_id` BIGINT NOT NULL COMMENT '用户ID',
  `llm_config_id` BIGINT NOT NULL COMMENT 'LLM配置ID',
  `title` VARCHAR(255) NOT NULL COMMENT '会话标题',
  `description` TEXT DEFAULT NULL COMMENT '会话描述',
  `is_active` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否激活',
  `message_count` INT NOT NULL DEFAULT 0 COMMENT '消息数',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  INDEX `idx_project_id` (`project_id`),
  INDEX `idx_user_id` (`user_id`),
  INDEX `idx_project_user` (`project_id`, `user_id`),
  FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`llm_config_id`) REFERENCES `llm_model_config`(`config_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识库对话会话表';

-- 15. 知识库对话消息表
CREATE TABLE IF NOT EXISTS `chat_message` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '消息ID',
  `session_id` BIGINT NOT NULL COMMENT '会话ID',
  `role` VARCHAR(32) NOT NULL COMMENT '角色(user/assistant)',
  `content` TEXT NOT NULL COMMENT '消息内容',
  `status` VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT '消息状态: pending/completed/interrupted/error',
  `duration_ms` INT DEFAULT NULL COMMENT '生成耗时(毫秒)',
  `thinking_log` TEXT DEFAULT NULL COMMENT '思考过程(JSON数组)',
  `referenced_files` TEXT DEFAULT NULL COMMENT '参考文件(JSON数组)',
  `tokens_used` INT DEFAULT NULL COMMENT '消耗的token数',
  `is_deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否已删除',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  INDEX `idx_session_id` (`session_id`),
  FOREIGN KEY (`session_id`) REFERENCES `chat_session`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识库对话消息表';

-- 16. 项目向量化任务表
CREATE TABLE IF NOT EXISTS `project_vectorization_task` (
  `id` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '任务ID',
  `task_id` VARCHAR(64) NOT NULL COMMENT '任务唯一标识',
  `project_id` BIGINT NOT NULL COMMENT '项目ID',
  `user_id` BIGINT NOT NULL COMMENT '触发用户ID',
  `task_type` VARCHAR(32) NOT NULL COMMENT '任务类型：incremental/full',
  `status` VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT '任务状态：pending/running/success/failed',
  `total` INT NOT NULL DEFAULT 0 COMMENT '文件总数',
  `processed` INT NOT NULL DEFAULT 0 COMMENT '处理成功数',
  `skipped` INT NOT NULL DEFAULT 0 COMMENT '跳过数',
  `failed` INT NOT NULL DEFAULT 0 COMMENT '失败数',
  `error_message` TEXT DEFAULT NULL COMMENT '错误信息',
  `started_at` DATETIME DEFAULT NULL COMMENT '开始时间',
  `finished_at` DATETIME DEFAULT NULL COMMENT '完成时间',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  UNIQUE KEY `uk_vector_task_id` (`task_id`),
  INDEX `idx_vector_task_project_status` (`project_id`, `status`),
  INDEX `idx_vector_task_user_id` (`user_id`),
  INDEX `idx_vector_task_created_at` (`created_at`),
  FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='项目向量化任务表';

-- 插入初始角色数据
INSERT INTO `roles` (`role_name`, `role_code`, `description`, `is_system`) VALUES
('超级管理员', 'super_admin', '拥有系统所有权限', 1),
('项目管理员', 'project_admin', '可以创建和管理项目', 1),
('普通用户', 'user', '可以查看和编辑被授权的项目', 1);

-- 插入初始菜单数据
INSERT INTO `system_menus` (`id`, `parent_id`, `menu_name`, `menu_code`, `menu_type`, `path`, `icon`, `sort_order`, `permission`) VALUES
(1, 0, '项目管理', 'project', 1, NULL, 'FolderOutlined', 1, NULL),
(2, 1, '我的项目', 'projects:my', 2, '/projects', NULL, 1, 'project:view'),
(3, 1, '创建项目', 'project', 3, NULL, NULL, 2, 'project:create'),
(4, 1, '编辑项目', 'project:edit', 3, NULL, NULL, 3, 'project:edit'),
(5, 1, '删除项目', 'project:delete', 3, NULL, NULL, 4, 'project:delete'),
(10, 0, '知识库管理', 'knowledge', 1, NULL, 'FileTextOutlined', 2, NULL),
(11, 10, '我的知识库', 'knowledge:view', 2, '/chat', 'CommentOutlined', 1, 'knowledge:view'),
(12, 10, '编辑知识库', 'knowledge:edit', 3, NULL, NULL, 2, 'knowledge:edit'),
(13, 10, '删除知识库', 'knowledge:delete', 3, NULL, NULL, 3, 'knowledge:delete'),
(20, 0, '系统管理', 'system', 1, '/system', 'SettingOutlined', 3, NULL),
(21, 20, '用户管理', 'user_manage', 2, '/system/users', NULL, 1, 'system:user:view'),
(22, 20, '角色管理', 'role_manage', 2, '/system/roles', NULL, 2, 'system:role:view');

-- 创建默认管理员用户（密码: [REDACTED]）
INSERT INTO `users` (`username`, `password_hash`, `nickname`, `is_superuser`, `status`) VALUES
('admin', '$2b$12$gQlTcKxyQKRYQtTT0Db.mexAsAYTJre9G8OSlzl3H3gMNTfXijiZy', '系统管理员', 1, 1);

-- 将管理员用户分配超级管理员角色
INSERT INTO `user_roles` (`user_id`, `role_id`) VALUES (1, 1);

-- 为超级管理员角色授权所有菜单
INSERT INTO `role_menus` (`role_id`, `menu_id`)
SELECT 1, id FROM `system_menus`;

-- 完成
SELECT '数据库初始化完成！' AS message;
SELECT CONCAT('默认管理员账号: admin, 密码: [REDACTED] AS credentials;
