-- AI视频生成记录表
CREATE TABLE IF NOT EXISTS aigcspace.ai_video (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL,
    task_id VARCHAR(100) NOT NULL UNIQUE,
    model_name VARCHAR(100) NOT NULL,
    video_type VARCHAR(50) NOT NULL,
    input_params JSONB NOT NULL,
    prompt TEXT,
    actual_prompt TEXT,
    first_frame_url VARCHAR(500),
    last_frame_url VARCHAR(500),
    audio_url VARCHAR(500),
    video_url VARCHAR(500),
    video_duration NUMERIC(10, 2),
    resolution VARCHAR(20),
    status VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    error_message TEXT,
    bill NUMERIC(10, 4) DEFAULT 0,
    submit_time TIMESTAMP,
    scheduled_time TIMESTAMP,
    end_time TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_ai_video_user_id ON aigcspace.ai_video(user_id);
CREATE INDEX IF NOT EXISTS idx_ai_video_task_id ON aigcspace.ai_video(task_id);
CREATE INDEX IF NOT EXISTS idx_ai_video_status ON aigcspace.ai_video(status);
CREATE INDEX IF NOT EXISTS idx_ai_video_created_at ON aigcspace.ai_video(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_ai_video_video_type ON aigcspace.ai_video(video_type);

COMMENT ON TABLE aigcspace.ai_video IS 'AI视频生成记录表';
COMMENT ON COLUMN aigcspace.ai_video.video_type IS '视频类型：t2v=文生视频，i2v=图生视频，s2v=数字人合成';
COMMENT ON COLUMN aigcspace.ai_video.input_params IS '完整输入参数JSON，便于追溯';
COMMENT ON COLUMN aigcspace.ai_video.actual_prompt IS '开启prompt_extend时返回的改写后提示词';
