-- iMeeting latest schema initialization script
-- 用途:
--   1. 面向当前最新代码结构的全新部署
--   2. 不再兼容或保留旧 users / roles / menus 等历史结构
--   3. 仅负责建表，不写入基础角色/用户/菜单/参数等种子数据
--   4. 本文件即为全量建表入口，无需执行历史 migrations 目录脚本
--
-- 执行顺序:
--   1. 先执行本文件
--   2. 再执行 imeeting-seed-latest.sql

SET NAMES utf8mb4;

SET FOREIGN_KEY_CHECKS = 0;

-- 兼容旧迁移中创建的 legacy views
DROP VIEW IF EXISTS `users`;
DROP VIEW IF EXISTS `roles`;
DROP VIEW IF EXISTS `menus`;
DROP VIEW IF EXISTS `role_menu_permissions`;
DROP VIEW IF EXISTS `dict_data`;
DROP VIEW IF EXISTS `system_parameters`;

-- 清理旧结构与当前结构
DROP TABLE IF EXISTS `sys_role_menu_permissions`;
DROP TABLE IF EXISTS `prompt_config`;
DROP TABLE IF EXISTS `sys_user_mcp`;
DROP TABLE IF EXISTS `user_logs`;
DROP TABLE IF EXISTS `user_voiceprint`;
DROP TABLE IF EXISTS `client_downloads`;
DROP TABLE IF EXISTS `external_apps`;
DROP TABLE IF EXISTS `terminals`;
DROP TABLE IF EXISTS `knowledge_base_tasks`;
DROP TABLE IF EXISTS `knowledge_bases`;
DROP TABLE IF EXISTS `llm_tasks`;
DROP TABLE IF EXISTS `transcript_segments`;
DROP TABLE IF EXISTS `transcript_tasks`;
DROP TABLE IF EXISTS `audio_files`;
DROP TABLE IF EXISTS `attachments`;
DROP TABLE IF EXISTS `attendees`;
DROP TABLE IF EXISTS `meetings`;
DROP TABLE IF EXISTS `tags`;
DROP TABLE IF EXISTS `prompts`;
DROP TABLE IF EXISTS `audio_model_config`;
DROP TABLE IF EXISTS `llm_model_config`;
DROP TABLE IF EXISTS `hot_word_item`;
DROP TABLE IF EXISTS `hot_word_group`;
DROP TABLE IF EXISTS `sys_system_parameters`;
DROP TABLE IF EXISTS `sys_dict_data`;
DROP TABLE IF EXISTS `sys_menus`;
DROP TABLE IF EXISTS `sys_users`;
DROP TABLE IF EXISTS `sys_roles`;

-- 历史阶段遗留表，一并清理
DROP TABLE IF EXISTS `meeting_summaries`;
DROP TABLE IF EXISTS `sys_user_prompt_config`;
DROP TABLE IF EXISTS `ai_model_configs`;
DROP TABLE IF EXISTS `ai_model_config`;
DROP TABLE IF EXISTS `llm_model_configs`;
DROP TABLE IF EXISTS `audio_model_configs`;
DROP TABLE IF EXISTS `hot_words`;
DROP TABLE IF EXISTS `system_parameters`;
DROP TABLE IF EXISTS `dict_data`;
DROP TABLE IF EXISTS `role_menu_permissions`;
DROP TABLE IF EXISTS `menus`;
DROP TABLE IF EXISTS `users`;
DROP TABLE IF EXISTS `roles`;

SET FOREIGN_KEY_CHECKS = 1;

-- =====================================================================
-- 1. 基础系统表
-- =====================================================================
CREATE TABLE `sys_roles` (
  `role_id` INT NOT NULL AUTO_INCREMENT,
  `role_name` VARCHAR(50) NOT NULL COMMENT '角色名称',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`role_id`),
  UNIQUE KEY `uk_sys_roles_name` (`role_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统角色表';

CREATE TABLE `sys_users` (
  `user_id` INT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(50) NOT NULL COMMENT '登录账号',
  `caption` VARCHAR(50) NOT NULL COMMENT '显示名称',
  `email` VARCHAR(100) DEFAULT NULL COMMENT '邮箱，可为空',
  `avatar_url` VARCHAR(255) DEFAULT NULL COMMENT '头像地址',
  `password_hash` VARCHAR(255) NOT NULL COMMENT '密码哈希',
  `role_id` INT NOT NULL DEFAULT 2 COMMENT '角色ID',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `uk_sys_users_username` (`username`),
  UNIQUE KEY `uk_sys_users_email` (`email`),
  KEY `idx_sys_users_role_id` (`role_id`),
  KEY `idx_sys_users_created_at` (`created_at`),
  CONSTRAINT `fk_sys_users_role_id`
    FOREIGN KEY (`role_id`) REFERENCES `sys_roles` (`role_id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统用户表';

CREATE TABLE `sys_menus` (
  `menu_id` INT NOT NULL AUTO_INCREMENT,
  `menu_code` VARCHAR(50) NOT NULL COMMENT '菜单编码',
  `menu_name` VARCHAR(100) NOT NULL COMMENT '菜单名称',
  `menu_icon` VARCHAR(100) DEFAULT NULL COMMENT '菜单图标',
  `menu_url` VARCHAR(255) DEFAULT NULL COMMENT '前端路由',
  `menu_type` ENUM('action', 'link', 'divider') NOT NULL DEFAULT 'link' COMMENT '菜单类型',
  `parent_id` INT DEFAULT NULL COMMENT '父菜单ID',
  `menu_level` TINYINT NOT NULL DEFAULT 1 COMMENT '菜单层级（根节点为1）',
  `tree_path` VARCHAR(255) DEFAULT NULL COMMENT '树路径（如 /1/5）',
  `sort_order` INT NOT NULL DEFAULT 0 COMMENT '排序号',
  `is_active` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  `is_visible` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否在侧边栏显示',
  `description` VARCHAR(255) DEFAULT NULL COMMENT '菜单说明',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`menu_id`),
  UNIQUE KEY `uk_sys_menus_code` (`menu_code`),
  KEY `idx_sys_menus_parent_id` (`parent_id`),
  KEY `idx_sys_menus_menu_level` (`menu_level`),
  KEY `idx_sys_menus_tree_path` (`tree_path`),
  KEY `idx_sys_menus_is_active` (`is_active`),
  KEY `idx_sys_menus_is_visible` (`is_visible`),
  CONSTRAINT `fk_sys_menus_parent_id`
    FOREIGN KEY (`parent_id`) REFERENCES `sys_menus` (`menu_id`)
    ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统菜单表';

CREATE TABLE `sys_dict_data` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `dict_type` VARCHAR(64) NOT NULL DEFAULT 'client_platform' COMMENT '字典类型',
  `dict_code` VARCHAR(64) NOT NULL COMMENT '业务编码',
  `parent_code` VARCHAR(64) NOT NULL DEFAULT 'ROOT' COMMENT '父级编码',
  `tree_path` VARCHAR(255) DEFAULT NULL COMMENT '树路径',
  `label_cn` VARCHAR(128) NOT NULL COMMENT '中文名称',
  `label_en` VARCHAR(128) DEFAULT NULL COMMENT '英文名称',
  `sort_order` INT NOT NULL DEFAULT 0 COMMENT '排序',
  `extension_attr` LONGTEXT DEFAULT NULL COMMENT '扩展属性(JSON字符串)',
  `is_default` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否默认',
  `status` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '状态',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_sys_dict_data_type_code` (`dict_type`, `dict_code`),
  KEY `idx_sys_dict_data_type_parent` (`dict_type`, `parent_code`),
  KEY `idx_sys_dict_data_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统字典数据表';

CREATE TABLE `sys_system_parameters` (
  `param_id` BIGINT NOT NULL AUTO_INCREMENT,
  `param_key` VARCHAR(128) NOT NULL COMMENT '参数键',
  `param_name` VARCHAR(255) NOT NULL COMMENT '参数名称',
  `param_value` TEXT DEFAULT NULL COMMENT '参数值',
  `value_type` VARCHAR(32) NOT NULL DEFAULT 'string' COMMENT '值类型',
  `category` VARCHAR(64) NOT NULL DEFAULT 'system' COMMENT '参数分类',
  `description` VARCHAR(500) DEFAULT NULL COMMENT '参数说明',
  `is_active` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`param_id`),
  UNIQUE KEY `uk_sys_system_parameters_key` (`param_key`),
  KEY `idx_sys_system_parameters_category` (`category`),
  KEY `idx_sys_system_parameters_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统参数表';

CREATE TABLE `hot_word_group` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL COMMENT '热词组名称',
  `description` VARCHAR(500) DEFAULT NULL COMMENT '描述',
  `vocabulary_id` VARCHAR(255) DEFAULT NULL COMMENT '阿里云词表ID',
  `last_sync_time` DATETIME DEFAULT NULL COMMENT '最后同步时间',
  `status` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '状态',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_hot_word_group_name` (`name`),
  KEY `idx_hot_word_group_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='热词组主表';

CREATE TABLE `llm_model_config` (
  `config_id` BIGINT NOT NULL AUTO_INCREMENT,
  `model_code` VARCHAR(128) NOT NULL COMMENT '模型编码',
  `model_name` VARCHAR(255) NOT NULL COMMENT '模型名称',
  `provider` VARCHAR(64) DEFAULT NULL COMMENT '供应商',
  `endpoint_url` VARCHAR(512) DEFAULT NULL COMMENT '模型接口地址',
  `api_key` VARCHAR(512) DEFAULT NULL COMMENT '接口密钥',
  `llm_model_name` VARCHAR(128) NOT NULL COMMENT '供应商模型名',
  `llm_timeout` INT NOT NULL DEFAULT 120 COMMENT '超时时间(秒)',
  `llm_temperature` DECIMAL(5,2) NOT NULL DEFAULT 0.70 COMMENT 'temperature',
  `llm_top_p` DECIMAL(5,2) NOT NULL DEFAULT 0.90 COMMENT 'top_p',
  `llm_max_tokens` INT NOT NULL DEFAULT 2048 COMMENT '最大token数',
  `llm_system_prompt` TEXT DEFAULT 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 NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`config_id`),
  UNIQUE KEY `uk_llm_model_config_code` (`model_code`),
  KEY `idx_llm_model_config_active` (`is_active`),
  KEY `idx_llm_model_config_default` (`is_default`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='LLM模型配置表';

CREATE TABLE `audio_model_config` (
  `config_id` BIGINT NOT NULL AUTO_INCREMENT,
  `model_code` VARCHAR(128) NOT NULL COMMENT '模型编码',
  `model_name` VARCHAR(255) NOT NULL COMMENT '模型名称',
  `audio_scene` VARCHAR(32) NOT NULL COMMENT 'asr / voiceprint',
  `provider` VARCHAR(64) DEFAULT NULL COMMENT '供应商',
  `endpoint_url` VARCHAR(512) DEFAULT NULL COMMENT '接口地址',
  `api_key` VARCHAR(512) DEFAULT NULL COMMENT '接口密钥',
  `request_timeout_seconds` INT NOT NULL DEFAULT 300 COMMENT '请求超时(秒)',
  `hot_word_group_id` INT DEFAULT NULL COMMENT '关联热词组',
  `extra_config` JSON DEFAULT 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 NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`config_id`),
  UNIQUE KEY `uk_audio_model_config_code` (`model_code`),
  KEY `idx_audio_model_config_scene` (`audio_scene`),
  KEY `idx_audio_model_config_active` (`is_active`),
  KEY `idx_audio_model_config_default` (`is_default`),
  KEY `idx_audio_model_config_hot_word_group_id` (`hot_word_group_id`),
  CONSTRAINT `fk_audio_model_config_hot_word_group_id`
    FOREIGN KEY (`hot_word_group_id`) REFERENCES `hot_word_group` (`id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='音频模型配置表';

CREATE TABLE `hot_word_item` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `group_id` INT NOT NULL COMMENT '热词组ID',
  `text` VARCHAR(255) NOT NULL COMMENT '热词内容',
  `weight` INT NOT NULL DEFAULT 4 COMMENT '权重',
  `lang` VARCHAR(20) NOT NULL DEFAULT 'zh' COMMENT '语言',
  `status` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '状态',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_hot_word_item_group_text` (`group_id`, `text`),
  KEY `idx_hot_word_item_group_id` (`group_id`),
  KEY `idx_hot_word_item_status` (`status`),
  CONSTRAINT `fk_hot_word_item_group_id`
    FOREIGN KEY (`group_id`) REFERENCES `hot_word_group` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='热词条目表';

CREATE TABLE `prompts` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL COMMENT '提示词名称',
  `task_type` ENUM('MEETING_TASK', 'KNOWLEDGE_TASK') NOT NULL COMMENT '任务类型',
  `content` LONGTEXT NOT NULL COMMENT '提示词内容',
  `desc` VARCHAR(500) DEFAULT NULL COMMENT '模版简介',
  `is_default` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否默认',
  `is_active` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  `creator_id` INT NOT NULL COMMENT '创建者',
  `is_system` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否系统模版',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_prompts_creator_id` (`creator_id`),
  KEY `idx_prompts_task_scope_active` (`task_type`, `is_system`, `creator_id`, `is_active`, `is_default`),
  CONSTRAINT `fk_prompts_creator_id`
    FOREIGN KEY (`creator_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='提示词模版表';

CREATE TABLE `prompt_config` (
  `config_id` BIGINT NOT NULL AUTO_INCREMENT,
  `user_id` INT NOT NULL COMMENT '用户ID',
  `task_type` ENUM('MEETING_TASK', 'KNOWLEDGE_TASK') NOT NULL COMMENT '任务类型',
  `prompt_id` INT NOT NULL COMMENT '模版ID',
  `is_enabled` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  `sort_order` INT NOT NULL DEFAULT 0 COMMENT '排序',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`config_id`),
  UNIQUE KEY `uk_prompt_config_user_task_prompt` (`user_id`, `task_type`, `prompt_id`),
  KEY `idx_prompt_config_user_task_order` (`user_id`, `task_type`, `sort_order`),
  KEY `idx_prompt_config_prompt_id` (`prompt_id`),
  CONSTRAINT `fk_prompt_config_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT,
  CONSTRAINT `fk_prompt_config_prompt_id`
    FOREIGN KEY (`prompt_id`) REFERENCES `prompts` (`id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户提示词配置表';

CREATE TABLE `sys_role_menu_permissions` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `role_id` INT NOT NULL COMMENT '角色ID',
  `menu_id` INT NOT NULL COMMENT '菜单ID',
  `granted_by` INT DEFAULT NULL COMMENT '授权人ID',
  `granted_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '授权时间',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_sys_role_menu_permissions_role_menu` (`role_id`, `menu_id`),
  KEY `idx_sys_role_menu_permissions_role_id` (`role_id`),
  KEY `idx_sys_role_menu_permissions_menu_id` (`menu_id`),
  KEY `idx_sys_role_menu_permissions_granted_by` (`granted_by`),
  KEY `idx_sys_role_menu_permissions_granted_at` (`granted_at`),
  CONSTRAINT `fk_sys_role_menu_permissions_role_id`
    FOREIGN KEY (`role_id`) REFERENCES `sys_roles` (`role_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT,
  CONSTRAINT `fk_sys_role_menu_permissions_menu_id`
    FOREIGN KEY (`menu_id`) REFERENCES `sys_menus` (`menu_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT,
  CONSTRAINT `fk_sys_role_menu_permissions_granted_by`
    FOREIGN KEY (`granted_by`) REFERENCES `sys_users` (`user_id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色菜单授权表';

CREATE TABLE `sys_user_mcp` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `user_id` INT NOT NULL COMMENT '用户ID',
  `bot_id` VARCHAR(64) NOT NULL COMMENT '机器人ID',
  `bot_secret` VARCHAR(128) NOT NULL COMMENT '机器人密钥',
  `status` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '状态',
  `last_used_at` DATETIME DEFAULT NULL COMMENT '最后使用时间',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_sys_user_mcp_user_id` (`user_id`),
  UNIQUE KEY `uk_sys_user_mcp_bot_id` (`bot_id`),
  KEY `idx_sys_user_mcp_status` (`status`),
  CONSTRAINT `fk_sys_user_mcp_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户MCP接入凭证表';

-- =====================================================================
-- 2. 业务表
-- =====================================================================
CREATE TABLE `tags` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL COMMENT '标签名称',
  `color` VARCHAR(7) NOT NULL DEFAULT '#409EFF' COMMENT '标签颜色',
  `creator_id` INT DEFAULT NULL COMMENT '创建人',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tags_name` (`name`),
  KEY `idx_tags_creator_id` (`creator_id`),
  CONSTRAINT `fk_tags_creator_id`
    FOREIGN KEY (`creator_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='标签表';

CREATE TABLE `meetings` (
  `meeting_id` INT NOT NULL AUTO_INCREMENT,
  `user_id` INT NOT NULL COMMENT '创建人',
  `title` VARCHAR(255) NOT NULL COMMENT '会议标题',
  `tags` VARCHAR(255) DEFAULT NULL COMMENT '逗号分隔标签',
  `meeting_time` DATETIME DEFAULT NULL COMMENT '会议时间',
  `access_password` VARCHAR(32) DEFAULT NULL COMMENT '访问密码',
  `prompt_id` INT DEFAULT 0 COMMENT '选用提示词ID，0表示未指定',
  `user_prompt` TEXT DEFAULT NULL COMMENT '用户额外提示',
  `summary` LONGTEXT DEFAULT NULL COMMENT '会议总结',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`meeting_id`),
  KEY `idx_meetings_user_id` (`user_id`),
  KEY `idx_meetings_meeting_time` (`meeting_time`),
  KEY `idx_meetings_created_at` (`created_at`),
  KEY `idx_meetings_prompt_id` (`prompt_id`),
  CONSTRAINT `fk_meetings_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会议表';

CREATE TABLE `attendees` (
  `attendee_id` INT NOT NULL AUTO_INCREMENT,
  `meeting_id` INT NOT NULL COMMENT '会议ID',
  `user_id` INT NOT NULL COMMENT '参会用户ID',
  PRIMARY KEY (`attendee_id`),
  UNIQUE KEY `uk_attendees_meeting_user` (`meeting_id`, `user_id`),
  KEY `idx_attendees_meeting_id` (`meeting_id`),
  KEY `idx_attendees_user_id` (`user_id`),
  CONSTRAINT `fk_attendees_meeting_id`
    FOREIGN KEY (`meeting_id`) REFERENCES `meetings` (`meeting_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT,
  CONSTRAINT `fk_attendees_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会议参会人表';

CREATE TABLE `attachments` (
  `attachment_id` INT NOT NULL AUTO_INCREMENT,
  `meeting_id` INT NOT NULL COMMENT '会议ID',
  `file_name` VARCHAR(255) NOT NULL COMMENT '文件名',
  `file_path` VARCHAR(512) NOT NULL COMMENT '文件路径',
  `file_type` VARCHAR(100) DEFAULT NULL COMMENT '文件类型',
  `uploaded_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '上传时间',
  PRIMARY KEY (`attachment_id`),
  KEY `idx_attachments_meeting_id` (`meeting_id`),
  CONSTRAINT `fk_attachments_meeting_id`
    FOREIGN KEY (`meeting_id`) REFERENCES `meetings` (`meeting_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会议附件表';

CREATE TABLE `audio_files` (
  `audio_id` INT NOT NULL AUTO_INCREMENT,
  `meeting_id` INT NOT NULL COMMENT '会议ID',
  `file_path` VARCHAR(512) NOT NULL COMMENT '音频相对路径',
  `file_name` VARCHAR(255) DEFAULT NULL COMMENT '原始文件名',
  `file_size` BIGINT DEFAULT NULL COMMENT '文件大小(字节)',
  `duration` DECIMAL(10,2) DEFAULT NULL COMMENT '音频时长(秒)',
  `upload_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '上传时间',
  `processing_status` VARCHAR(20) NOT NULL DEFAULT 'uploaded' COMMENT '处理状态',
  `error_message` TEXT DEFAULT NULL COMMENT '错误信息',
  `task_id` VARCHAR(255) DEFAULT NULL COMMENT '最新转写任务ID',
  PRIMARY KEY (`audio_id`),
  UNIQUE KEY `uk_audio_files_meeting_id` (`meeting_id`),
  KEY `idx_audio_files_task_id` (`task_id`),
  KEY `idx_audio_files_processing_status` (`processing_status`),
  CONSTRAINT `fk_audio_files_meeting_id`
    FOREIGN KEY (`meeting_id`) REFERENCES `meetings` (`meeting_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会议音频表';

CREATE TABLE `transcript_tasks` (
  `task_id` VARCHAR(100) NOT NULL COMMENT '业务任务ID',
  `paraformer_task_id` VARCHAR(100) DEFAULT NULL COMMENT '云端任务ID',
  `meeting_id` INT NOT NULL COMMENT '会议ID',
  `status` ENUM('pending', 'processing', 'completed', 'failed') NOT NULL DEFAULT 'pending' COMMENT '任务状态',
  `progress` INT NOT NULL DEFAULT 0 COMMENT '任务进度',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `completed_at` DATETIME DEFAULT NULL COMMENT '完成时间',
  `error_message` TEXT DEFAULT NULL COMMENT '错误信息',
  PRIMARY KEY (`task_id`),
  KEY `idx_transcript_tasks_meeting_id` (`meeting_id`),
  KEY `idx_transcript_tasks_status` (`status`),
  KEY `idx_transcript_tasks_created_at` (`created_at`),
  KEY `idx_transcript_tasks_paraformer_task_id` (`paraformer_task_id`),
  CONSTRAINT `fk_transcript_tasks_meeting_id`
    FOREIGN KEY (`meeting_id`) REFERENCES `meetings` (`meeting_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='转写任务表';

CREATE TABLE `transcript_segments` (
  `segment_id` BIGINT NOT NULL AUTO_INCREMENT,
  `meeting_id` INT NOT NULL COMMENT '会议ID',
  `speaker_id` INT NOT NULL DEFAULT 0 COMMENT '说话人编号',
  `speaker_tag` VARCHAR(50) DEFAULT NULL COMMENT '说话人标签',
  `start_time_ms` INT NOT NULL COMMENT '开始时间(毫秒)',
  `end_time_ms` INT NOT NULL COMMENT '结束时间(毫秒)',
  `text_content` LONGTEXT NOT NULL COMMENT '转写文本',
  PRIMARY KEY (`segment_id`),
  KEY `idx_transcript_segments_meeting_id` (`meeting_id`),
  KEY `idx_transcript_segments_speaker_id` (`speaker_id`),
  KEY `idx_transcript_segments_start_time_ms` (`start_time_ms`),
  CONSTRAINT `fk_transcript_segments_meeting_id`
    FOREIGN KEY (`meeting_id`) REFERENCES `meetings` (`meeting_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='转写分段表';

CREATE TABLE `llm_tasks` (
  `task_id` VARCHAR(100) NOT NULL COMMENT '总结任务ID',
  `meeting_id` INT NOT NULL COMMENT '会议ID',
  `user_prompt` TEXT DEFAULT NULL COMMENT '用户额外提示',
  `prompt_id` INT DEFAULT NULL COMMENT '使用的模版ID',
  `model_code` VARCHAR(128) DEFAULT NULL COMMENT '使用的模型编码',
  `status` ENUM('pending', 'processing', 'completed', 'failed') NOT NULL DEFAULT 'pending' COMMENT '任务状态',
  `progress` INT NOT NULL DEFAULT 0 COMMENT '任务进度',
  `result` LONGTEXT DEFAULT NULL COMMENT '总结结果',
  `error_message` TEXT DEFAULT NULL COMMENT '错误信息',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `completed_at` DATETIME DEFAULT NULL COMMENT '完成时间',
  PRIMARY KEY (`task_id`),
  KEY `idx_llm_tasks_meeting_id` (`meeting_id`),
  KEY `idx_llm_tasks_status` (`status`),
  KEY `idx_llm_tasks_created_at` (`created_at`),
  KEY `idx_llm_tasks_prompt_id` (`prompt_id`),
  KEY `idx_llm_tasks_model_code` (`model_code`),
  CONSTRAINT `fk_llm_tasks_meeting_id`
    FOREIGN KEY (`meeting_id`) REFERENCES `meetings` (`meeting_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='会议总结任务表';

CREATE TABLE `knowledge_bases` (
  `kb_id` INT NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(255) NOT NULL COMMENT '知识标题',
  `prompt_id` INT DEFAULT 0 COMMENT '使用的模版ID',
  `user_prompt` TEXT DEFAULT NULL COMMENT '用户额外提示',
  `content` LONGTEXT DEFAULT NULL COMMENT '知识内容',
  `creator_id` INT NOT NULL COMMENT '创建人',
  `is_shared` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否共享',
  `source_meeting_ids` VARCHAR(255) DEFAULT NULL COMMENT '来源会议ID列表',
  `tags` VARCHAR(255) DEFAULT NULL COMMENT '标签',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`kb_id`),
  KEY `idx_knowledge_bases_creator_id` (`creator_id`),
  KEY `idx_knowledge_bases_prompt_id` (`prompt_id`),
  KEY `idx_knowledge_bases_updated_at` (`updated_at`),
  KEY `idx_knowledge_bases_is_shared` (`is_shared`),
  CONSTRAINT `fk_knowledge_bases_creator_id`
    FOREIGN KEY (`creator_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='知识库表';

CREATE TABLE `knowledge_base_tasks` (
  `task_id` VARCHAR(100) NOT NULL COMMENT '知识库任务ID',
  `user_id` INT NOT NULL COMMENT '发起人',
  `kb_id` INT NOT NULL COMMENT '知识库ID',
  `prompt_id` INT DEFAULT NULL COMMENT '使用的模版ID',
  `user_prompt` TEXT DEFAULT NULL COMMENT '用户额外提示',
  `status` ENUM('pending', 'processing', 'completed', 'failed') NOT NULL DEFAULT 'pending' COMMENT '任务状态',
  `progress` INT NOT NULL DEFAULT 0 COMMENT '任务进度',
  `error_message` TEXT DEFAULT NULL COMMENT '错误信息',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `completed_at` DATETIME DEFAULT NULL COMMENT '完成时间',
  PRIMARY KEY (`task_id`),
  KEY `idx_knowledge_base_tasks_user_id` (`user_id`),
  KEY `idx_knowledge_base_tasks_kb_id` (`kb_id`),
  KEY `idx_knowledge_base_tasks_status` (`status`),
  KEY `idx_knowledge_base_tasks_created_at` (`created_at`),
  CONSTRAINT `fk_knowledge_base_tasks_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT,
  CONSTRAINT `fk_knowledge_base_tasks_kb_id`
    FOREIGN KEY (`kb_id`) REFERENCES `knowledge_bases` (`kb_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='知识库生成任务表';

CREATE TABLE `client_downloads` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `platform_type` VARCHAR(50) DEFAULT NULL COMMENT '平台类型(兼容字段)',
  `platform_name` VARCHAR(50) DEFAULT NULL COMMENT '平台名称(兼容字段)',
  `platform_code` VARCHAR(64) NOT NULL COMMENT '平台编码',
  `version` VARCHAR(50) NOT NULL COMMENT '版本号',
  `version_code` INT NOT NULL COMMENT '版本序号',
  `download_url` VARCHAR(512) NOT NULL COMMENT '下载地址',
  `file_size` BIGINT DEFAULT NULL COMMENT '文件大小',
  `release_notes` TEXT DEFAULT NULL COMMENT '更新说明',
  `is_active` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  `is_latest` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否最新',
  `min_system_version` VARCHAR(50) DEFAULT NULL COMMENT '最低系统版本',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `created_by` INT DEFAULT NULL COMMENT '创建人',
  PRIMARY KEY (`id`),
  KEY `idx_client_downloads_platform_code` (`platform_code`),
  KEY `idx_client_downloads_platform_type_name` (`platform_type`, `platform_name`),
  KEY `idx_client_downloads_is_latest` (`is_latest`),
  KEY `idx_client_downloads_is_active` (`is_active`),
  KEY `idx_client_downloads_platform_code_version` (`platform_code`, `version_code`),
  CONSTRAINT `fk_client_downloads_created_by`
    FOREIGN KEY (`created_by`) REFERENCES `sys_users` (`user_id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='客户端下载管理表';

CREATE TABLE `external_apps` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `app_name` VARCHAR(100) NOT NULL COMMENT '应用名称',
  `app_type` ENUM('native', 'web') NOT NULL COMMENT '应用类型',
  `app_info` TEXT DEFAULT NULL COMMENT '应用信息(JSON字符串)',
  `icon_url` TEXT DEFAULT NULL COMMENT '图标地址',
  `description` TEXT DEFAULT NULL COMMENT '应用说明',
  `sort_order` INT NOT NULL DEFAULT 0 COMMENT '排序',
  `is_active` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `created_by` INT DEFAULT NULL COMMENT '创建人',
  PRIMARY KEY (`id`),
  KEY `idx_external_apps_type` (`app_type`),
  KEY `idx_external_apps_active` (`is_active`),
  KEY `idx_external_apps_sort_order` (`sort_order`),
  KEY `idx_external_apps_type_active` (`app_type`, `is_active`),
  CONSTRAINT `fk_external_apps_created_by`
    FOREIGN KEY (`created_by`) REFERENCES `sys_users` (`user_id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='外部应用管理表';

CREATE TABLE `terminals` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `imei` VARCHAR(64) NOT NULL COMMENT '终端唯一标识',
  `terminal_name` VARCHAR(100) DEFAULT NULL COMMENT '终端名称',
  `terminal_type` VARCHAR(50) NOT NULL COMMENT '终端类型编码',
  `description` VARCHAR(500) DEFAULT NULL COMMENT '说明',
  `status` TINYINT(1) NOT NULL DEFAULT 1 COMMENT '启用状态',
  `is_activated` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '激活状态',
  `activated_at` DATETIME DEFAULT NULL COMMENT '激活时间',
  `firmware_version` VARCHAR(50) DEFAULT NULL COMMENT '固件版本',
  `last_online_at` DATETIME DEFAULT NULL COMMENT '最后在线时间',
  `ip_address` VARCHAR(50) DEFAULT NULL COMMENT '最近在线IP',
  `mac_address` VARCHAR(64) DEFAULT NULL COMMENT 'MAC地址',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `created_by` INT DEFAULT NULL COMMENT '创建人',
  `current_user_id` INT DEFAULT NULL COMMENT '当前绑定用户',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_terminals_imei` (`imei`),
  KEY `idx_terminals_terminal_type` (`terminal_type`),
  KEY `idx_terminals_status` (`status`),
  KEY `idx_terminals_created_by` (`created_by`),
  KEY `idx_terminals_current_user_id` (`current_user_id`),
  KEY `idx_terminals_last_online_at` (`last_online_at`),
  CONSTRAINT `fk_terminals_created_by`
    FOREIGN KEY (`created_by`) REFERENCES `sys_users` (`user_id`)
    ON DELETE SET NULL ON UPDATE RESTRICT,
  CONSTRAINT `fk_terminals_current_user_id`
    FOREIGN KEY (`current_user_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='专用终端设备表';

CREATE TABLE `user_logs` (
  `log_id` BIGINT NOT NULL AUTO_INCREMENT,
  `user_id` INT NOT NULL COMMENT '用户ID',
  `action_type` VARCHAR(50) NOT NULL COMMENT '行为类型',
  `ip_address` VARCHAR(50) DEFAULT NULL COMMENT 'IP地址',
  `user_agent` TEXT DEFAULT NULL COMMENT 'User-Agent',
  `metadata` LONGTEXT DEFAULT NULL COMMENT '扩展元数据(JSON字符串)',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`log_id`),
  KEY `idx_user_logs_user_id` (`user_id`),
  KEY `idx_user_logs_action_type` (`action_type`),
  KEY `idx_user_logs_created_at` (`created_at`),
  KEY `idx_user_logs_user_action` (`user_id`, `action_type`),
  CONSTRAINT `fk_user_logs_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户行为日志表';

CREATE TABLE `user_voiceprint` (
  `vp_id` INT NOT NULL AUTO_INCREMENT,
  `user_id` INT NOT NULL COMMENT '用户ID',
  `file_path` VARCHAR(255) NOT NULL COMMENT '声纹文件路径',
  `file_size` BIGINT DEFAULT NULL COMMENT '文件大小',
  `duration_seconds` DECIMAL(5,2) DEFAULT 10.00 COMMENT '音频时长',
  `vector_data` LONGTEXT DEFAULT NULL COMMENT '声纹向量(JSON字符串)',
  `collected_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '采集时间',
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`vp_id`),
  UNIQUE KEY `uk_user_voiceprint_user_id` (`user_id`),
  KEY `idx_user_voiceprint_collected_at` (`collected_at`),
  CONSTRAINT `fk_user_voiceprint_user_id`
    FOREIGN KEY (`user_id`) REFERENCES `sys_users` (`user_id`)
    ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户声纹表';

SELECT 'imeeting-schema-latest.sql executed successfully' AS `message`;
