"""Task 3.06: Create tts_jobs table

Revision ID: 020
Revises: 019
Create Date: 2026-07-02

新增 tts_jobs 表，用于存储 TTS 合成任务。
"""

import sqlalchemy as sa

from alembic import op

revision = "020"
down_revision = "019"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "tts_jobs",
        sa.Column("id", sa.String(36), primary_key=True),
        sa.Column("user_id", sa.String(36), nullable=False, index=True),
        sa.Column("input_text", sa.Text(), nullable=False),
        sa.Column("voice_id", sa.String(100), nullable=False, server_default=""),
        sa.Column("voice_model", sa.String(100), nullable=False, server_default=""),
        sa.Column("project_id", sa.String(36), nullable=False, server_default=""),
        sa.Column("voice_clone_profile_id", sa.String(36), nullable=False, server_default=""),
        sa.Column("status", sa.String(20), nullable=False, server_default="pending", index=True),
        sa.Column("output_audio_url", sa.Text(), nullable=False, server_default=""),
        sa.Column("output_audio_key", sa.String(500), nullable=False, server_default=""),
        sa.Column("duration", sa.Float(), nullable=False, server_default="0"),
        sa.Column("file_size", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("sample_rate", sa.Integer(), nullable=False, server_default="22050"),
        sa.Column("format", sa.String(20), nullable=False, server_default="mp3"),
        sa.Column("error_message", sa.Text(), nullable=False, server_default=""),
        sa.Column("retry_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("max_retries", sa.Integer(), nullable=False, server_default="3"),
        sa.Column("metadata", sa.JSON(), nullable=False, server_default="{}"),
        sa.Column("started_at", sa.DateTime(), nullable=True),
        sa.Column("completed_at", sa.DateTime(), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(),
            nullable=False,
            server_default=sa.func.now(),
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(),
            nullable=False,
            server_default=sa.func.now(),
        ),
    )


def downgrade() -> None:
    op.drop_table("tts_jobs")
