-- 手动创建training模块相关表的SQL脚本
-- 用于解决Alembic迁移失败的备选方案

-- 1. 创建training_scenes表
CREATE TABLE IF NOT EXISTS training_scenes (
    id INTEGER NOT NULL AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL COMMENT '场景名称',
    description TEXT COMMENT '场景描述',
    category VARCHAR(50) NOT NULL COMMENT '场景分类',
    ai_config JSON COMMENT 'AI配置（如Coze Bot ID等）',
    prompt_template TEXT COMMENT '提示词模板',
    evaluation_criteria JSON COMMENT '评估标准',
    status ENUM('DRAFT', 'ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'DRAFT' COMMENT '场景状态',
    is_public BOOLEAN NOT NULL DEFAULT TRUE COMMENT '是否公开',
    required_level INTEGER COMMENT '所需用户等级',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
    deleted_at DATETIME,
    created_by INTEGER,
    updated_by INTEGER,
    PRIMARY KEY (id),
    INDEX ix_training_scenes_id (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='陪练场景表';

-- 2. 创建training_sessions表
CREATE TABLE IF NOT EXISTS training_sessions (
    id INTEGER NOT NULL AUTO_INCREMENT,
    user_id BIGINT NOT NULL COMMENT '用户ID',
    scene_id INTEGER NOT NULL COMMENT '场景ID',
    coze_conversation_id VARCHAR(100) COMMENT 'Coze会话ID',
    start_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '开始时间',
    end_time DATETIME COMMENT '结束时间',
    duration_seconds INTEGER COMMENT '持续时长（秒）',
    status ENUM('CREATED', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED', 'ERROR') NOT NULL DEFAULT 'CREATED' COMMENT '会话状态',
    session_config JSON COMMENT '会话配置',
    total_score FLOAT COMMENT '总分',
    evaluation_result JSON COMMENT '评估结果详情',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    created_by INTEGER,
    updated_by INTEGER,
    PRIMARY KEY (id),
    INDEX ix_training_sessions_id (id),
    INDEX ix_training_sessions_user_id (user_id),
    FOREIGN KEY (scene_id) REFERENCES training_scenes(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='陪练会话表';

-- 3. 创建training_messages表
CREATE TABLE IF NOT EXISTS training_messages (
    id INTEGER NOT NULL AUTO_INCREMENT,
    session_id INTEGER NOT NULL COMMENT '会话ID',
    role ENUM('USER', 'ASSISTANT', 'SYSTEM') NOT NULL COMMENT '消息角色',
    type ENUM('TEXT', 'VOICE', 'SYSTEM') NOT NULL COMMENT '消息类型',
    content TEXT NOT NULL COMMENT '消息内容',
    voice_url VARCHAR(500) COMMENT '语音文件URL',
    voice_duration FLOAT COMMENT '语音时长（秒）',
    message_metadata JSON COMMENT '消息元数据',
    coze_message_id VARCHAR(100) COMMENT 'Coze消息ID',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    INDEX ix_training_messages_id (id),
    FOREIGN KEY (session_id) REFERENCES training_sessions(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='陪练消息表';

-- 4. 创建training_reports表
CREATE TABLE IF NOT EXISTS training_reports (
    id INTEGER NOT NULL AUTO_INCREMENT,
    session_id INTEGER NOT NULL COMMENT '会话ID',
    user_id BIGINT NOT NULL COMMENT '用户ID',
    overall_score FLOAT NOT NULL COMMENT '总体得分',
    dimension_scores JSON NOT NULL COMMENT '各维度得分',
    strengths JSON NOT NULL COMMENT '优势点',
    weaknesses JSON NOT NULL COMMENT '待改进点',
    suggestions JSON NOT NULL COMMENT '改进建议',
    detailed_analysis TEXT COMMENT '详细分析',
    transcript TEXT COMMENT '对话文本记录',
    statistics JSON COMMENT '统计数据',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    created_by INTEGER,
    updated_by INTEGER,
    PRIMARY KEY (id),
    UNIQUE KEY (session_id),
    INDEX ix_training_reports_id (id),
    INDEX ix_training_reports_user_id (user_id),
    FOREIGN KEY (session_id) REFERENCES training_sessions(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='陪练报告表';

-- 5. 更新Alembic版本表，标记迁移已完成
INSERT INTO alembic_version (version_num) VALUES ('9245f8845fe1') ON DUPLICATE KEY UPDATE version_num = '9245f8845fe1';
