-- 为现有数据库添加转录任务支持的SQL脚本

-- 1. 更新audio_files表结构，添加缺失字段
ALTER TABLE audio_files 
ADD COLUMN file_name VARCHAR(255) AFTER meeting_id,
ADD COLUMN file_size BIGINT DEFAULT NULL AFTER file_path,
ADD COLUMN task_id VARCHAR(255) DEFAULT NULL AFTER upload_time;

-- 2. 创建转录任务表
CREATE TABLE transcript_tasks (
    task_id VARCHAR(255) PRIMARY KEY,
    meeting_id INT NOT NULL,
    status ENUM('pending', 'processing', 'completed', 'failed') DEFAULT 'pending',
    progress INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP NULL,
    error_message TEXT NULL,
    
    FOREIGN KEY (meeting_id) REFERENCES meetings(meeting_id) ON DELETE CASCADE
);

-- 3. 添加索引以优化查询性能
-- audio_files 表索引
ALTER TABLE audio_files ADD INDEX idx_task_id (task_id);

-- transcript_tasks 表索引  
ALTER TABLE transcript_tasks ADD INDEX idx_meeting_id (meeting_id);
ALTER TABLE transcript_tasks ADD INDEX idx_status (status);
ALTER TABLE transcript_tasks ADD INDEX idx_created_at (created_at);

-- 4. 更新现有测试数据（如果需要）
-- 这些语句是可选的，用于更新现有的测试数据
UPDATE audio_files SET file_name = 'test_audio.mp3' WHERE file_name IS NULL;
UPDATE audio_files SET file_size = 10485760 WHERE file_size IS NULL; -- 10MB

SELECT '转录任务表创建完成！' as message;


CREATE TABLE llm_tasks (
    task_id VARCHAR(100) PRIMARY KEY,
    llm_task_id VARCHAR(100) DEFAULT NULL,
    meeting_id INT NOT NULL,
    user_prompt TEXT,
    status VARCHAR(50) DEFAULT 'pending',
    progress INT DEFAULT 0,
    result TEXT,
    error_message TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP NULL,
    INDEX idx_meeting_id (meeting_id),
    INDEX idx_status (status),
    INDEX idx_created_at (created_at)
)