-- 陪练分析报告功能数据库表
-- 创建时间：2025-10-13

-- 1. 陪练会话表
CREATE TABLE IF NOT EXISTS `practice_sessions` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `session_id` VARCHAR(50) NOT NULL UNIQUE COMMENT '会话ID（如PS006）',
  `user_id` INT NOT NULL COMMENT '学员ID',
  `scene_id` INT COMMENT '场景ID',
  `scene_name` VARCHAR(200) COMMENT '场景名称',
  `scene_type` VARCHAR(50) COMMENT '场景类型：phone/face/complaint等',
  `conversation_id` VARCHAR(100) COMMENT 'Coze对话ID',
  
  -- 会话时间信息
  `start_time` DATETIME NOT NULL COMMENT '开始时间',
  `end_time` DATETIME COMMENT '结束时间',
  `duration_seconds` INT DEFAULT 0 COMMENT '时长（秒）',
  `turns` INT DEFAULT 0 COMMENT '对话轮次',
  `status` VARCHAR(20) DEFAULT 'in_progress' COMMENT '状态：in_progress/completed/canceled',
  
  -- 审计字段
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` BOOLEAN DEFAULT FALSE,
  
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  FOREIGN KEY (scene_id) REFERENCES practice_scenes(id) ON DELETE SET NULL,
  INDEX idx_user_id (user_id),
  INDEX idx_session_id (session_id),
  INDEX idx_start_time (start_time),
  INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='陪练会话表';

-- 2. 对话记录表
CREATE TABLE IF NOT EXISTS `practice_dialogues` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `session_id` VARCHAR(50) NOT NULL COMMENT '会话ID',
  `speaker` VARCHAR(20) NOT NULL COMMENT '说话人：user/ai',
  `content` TEXT NOT NULL COMMENT '对话内容',
  `timestamp` DATETIME NOT NULL COMMENT '时间戳',
  `sequence` INT NOT NULL COMMENT '顺序号（从1开始）',
  
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  
  INDEX idx_session_id (session_id),
  INDEX idx_sequence (session_id, sequence)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='陪练对话记录表';

-- 3. 分析报告表
CREATE TABLE IF NOT EXISTS `practice_reports` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `session_id` VARCHAR(50) NOT NULL UNIQUE COMMENT '会话ID',
  
  -- AI分析结果（JSON存储）
  `total_score` INT COMMENT '综合得分（0-100）',
  `score_breakdown` JSON COMMENT '分数细分：开场技巧、需求挖掘等',
  `ability_dimensions` JSON COMMENT '能力维度：沟通表达、倾听理解等',
  `dialogue_review` JSON COMMENT '对话复盘（标注亮点话术/金牌话术）',
  `suggestions` JSON COMMENT '改进建议',
  
  -- Dify工作流信息
  `workflow_run_id` VARCHAR(100) COMMENT 'Dify工作流运行ID',
  `task_id` VARCHAR(100) COMMENT 'Dify任务ID',
  
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  INDEX idx_session_id (session_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='陪练分析报告表';

-- 验证表创建
SELECT 
  TABLE_NAME, 
  TABLE_ROWS, 
  TABLE_COMMENT 
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = 'kaopeilian' 
  AND TABLE_NAME IN ('practice_sessions', 'practice_dialogues', 'practice_reports');

