-- transcribe_task 语音转文字任务表（批次头）
CREATE TABLE IF NOT EXISTS transcribe_task (
    id BIGSERIAL NOT NULL,
    task_id VARCHAR(64) NOT NULL,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    status VARCHAR(20) NOT NULL DEFAULT 'pending',
    progress INT NOT NULL DEFAULT 0,
    total_files INT NOT NULL DEFAULT 0,
    success_files INT NOT NULL DEFAULT 0,
    fail_files INT NOT NULL DEFAULT 0,
    model VARCHAR(32) NOT NULL DEFAULT 'medium',
    language VARCHAR(10) NOT NULL DEFAULT 'zh',
    threshold DECIMAL(4,2) NOT NULL DEFAULT 0.30,
    input_type VARCHAR(10) NOT NULL DEFAULT 'upload',
    input_data TEXT,
    file_names TEXT,
    callback_url TEXT,
    result TEXT,
    error_message TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,
    PRIMARY KEY (id)
);

COMMENT ON TABLE transcribe_task IS '语音转文字异步任务批次头表';
COMMENT ON COLUMN transcribe_task.id IS '主键ID';
COMMENT ON COLUMN transcribe_task.task_id IS '任务批次唯一标识';
COMMENT ON COLUMN transcribe_task.tenant_id IS '租户ID';
COMMENT ON COLUMN transcribe_task.status IS '任务状态:pending等待/running处理中/success成功/failed失败';
COMMENT ON COLUMN transcribe_task.progress IS '处理进度(0-100)';
COMMENT ON COLUMN transcribe_task.total_files IS '文件总数';
COMMENT ON COLUMN transcribe_task.success_files IS '成功文件数';
COMMENT ON COLUMN transcribe_task.fail_files IS '失败文件数';
COMMENT ON COLUMN transcribe_task.model IS 'whisper模型:tiny/base/small/medium/large';
COMMENT ON COLUMN transcribe_task.language IS '语言代码:zh/en/ja等';
COMMENT ON COLUMN transcribe_task.threshold IS '场景检测阈值(0.1-0.5)';
COMMENT ON COLUMN transcribe_task.input_type IS '输入类型:upload文件上传/url远程URL';
COMMENT ON COLUMN transcribe_task.input_data IS '输入数据JSON:上传模式存文件路径列表,URL模式存URL列表';
COMMENT ON COLUMN transcribe_task.file_names IS '文件名列表JSON,用于展示';
COMMENT ON COLUMN transcribe_task.callback_url IS '任务完成后的回调地址(可选)';
COMMENT ON COLUMN transcribe_task.result IS '完整的处理结果JSON(成功后填充)';
COMMENT ON COLUMN transcribe_task.error_message IS '错误信息,失败后填充';
COMMENT ON COLUMN transcribe_task.created_at IS '创建时间';
COMMENT ON COLUMN transcribe_task.updated_at IS '更新时间';
COMMENT ON COLUMN transcribe_task.deleted_at IS '删除时间(软删除)';

CREATE UNIQUE INDEX IF NOT EXISTS idx_transcribe_task_task_id ON transcribe_task(task_id);
CREATE INDEX IF NOT EXISTS idx_transcribe_task_status ON transcribe_task(status);
CREATE INDEX IF NOT EXISTS idx_transcribe_task_created_at ON transcribe_task(created_at);

-- transcribe_task_detail 语音转文字任务明细表（每个视频一条）
CREATE TABLE IF NOT EXISTS transcribe_task_detail (
    id BIGSERIAL NOT NULL,
    task_id VARCHAR(64) NOT NULL,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    file_index INT NOT NULL DEFAULT 0,
    file_name VARCHAR(255) NOT NULL DEFAULT '',
    transcribed_text TEXT,
    scenes TEXT,
    audio_size BIGINT DEFAULT 0,
    audio_duration VARCHAR(32) DEFAULT '',
    model VARCHAR(32) DEFAULT '',
    language VARCHAR(10) DEFAULT '',
    error_message TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    PRIMARY KEY (id)
);

COMMENT ON TABLE transcribe_task_detail IS '语音转文字任务明细表(每视频一条)';
COMMENT ON COLUMN transcribe_task_detail.id IS '主键ID';
COMMENT ON COLUMN transcribe_task_detail.task_id IS '所属任务批次ID';
COMMENT ON COLUMN transcribe_task_detail.tenant_id IS '租户ID';
COMMENT ON COLUMN transcribe_task_detail.file_index IS '文件在批次中的序号(从0开始)';
COMMENT ON COLUMN transcribe_task_detail.file_name IS '文件名';
COMMENT ON COLUMN transcribe_task_detail.transcribed_text IS '语音识别文字';
COMMENT ON COLUMN transcribe_task_detail.scenes IS '分镜分析JSON';
COMMENT ON COLUMN transcribe_task_detail.audio_size IS '音频文件大小(字节)';
COMMENT ON COLUMN transcribe_task_detail.audio_duration IS '音频时长';
COMMENT ON COLUMN transcribe_task_detail.model IS 'whisper模型';
COMMENT ON COLUMN transcribe_task_detail.language IS '语言代码';
COMMENT ON COLUMN transcribe_task_detail.error_message IS '错误信息,失败后填充';
COMMENT ON COLUMN transcribe_task_detail.created_at IS '创建时间';

CREATE INDEX IF NOT EXISTS idx_transcribe_task_detail_task_id ON transcribe_task_detail(task_id);
CREATE INDEX IF NOT EXISTS idx_transcribe_task_detail_file_idx ON transcribe_task_detail(task_id, file_index);
