-- 创建QA会话表
CREATE TABLE IF NOT EXISTS `t_qa_session` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `session_id` varchar(100) NOT NULL COMMENT '会话ID',
    `project_id` varchar(50) NOT NULL COMMENT '项目ID',
    `user_id` varchar(50) NOT NULL COMMENT '用户ID',
    `first_query` text COMMENT '首次问题内容',
    `start_time` datetime NOT NULL COMMENT '会话开始时间',
    `last_message_time` datetime DEFAULT NULL COMMENT '最后消息时间',
    `message_count` int DEFAULT '0' COMMENT '消息总数',
    `status` varchar(20) DEFAULT 'active' COMMENT '会话状态：active-活跃，closed-关闭',
    `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_session_id` (`session_id`),
    KEY `idx_project_user` (`project_id`, `user_id`),
    KEY `idx_start_time` (`start_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='QA会话表';

-- 创建QA消息表
CREATE TABLE IF NOT EXISTS `t_qa_message` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `message_id` varchar(100) NOT NULL COMMENT '消息ID',
    `session_id` varchar(100) NOT NULL COMMENT '会话ID',
    `sender` varchar(20) NOT NULL COMMENT '发送者：user-用户，ai-AI',
    `content` text NOT NULL COMMENT '消息内容',
    `sources` text COMMENT '来源信息JSON',
    `file_ids` text COMMENT '相关文件ID列表JSON',
    `timestamp` datetime NOT NULL COMMENT '消息时间戳',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_message_id` (`message_id`),
    KEY `idx_session_timestamp` (`session_id`, `timestamp`),
    KEY `idx_sender` (`sender`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='QA消息表';

-- 创建文件目录表
CREATE TABLE IF NOT EXISTS `t_file_directory` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `file_id` varchar(100) NOT NULL COMMENT '文件ID',
    `chapter_id` varchar(100) NOT NULL COMMENT '章节ID',
    `title` text NOT NULL COMMENT '章节标题',
    `page` varchar(50) COMMENT '页码信息',
    `start_page` int COMMENT '起始页码',
    `end_page` int COMMENT '结束页码',
    `parent_chapter_id` varchar(100) COMMENT '父章节ID',
    `level` int DEFAULT '1' COMMENT '层级',
    `sort_order` int DEFAULT '0' COMMENT '排序',
    `summary` text 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_file_chapter` (`file_id`, `chapter_id`),
    KEY `idx_file_id` (`file_id`),
    KEY `idx_parent_chapter` (`parent_chapter_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='文件目录表';
