"""Phase 8 任务 2.10: Create jobs table for unified async task management

Revision ID: 018
Revises: 017
Create Date: 2026-07-01

新增 jobs 表，用于统一管理异步任务（视频合成、渲染等）的生命周期。
"""

import sqlalchemy as sa

from alembic import op

revision = "018"
down_revision = "017"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "jobs",
        sa.Column("id", sa.String(32), primary_key=True),
        sa.Column("project_id", sa.String(32), nullable=False, index=True),
        sa.Column("job_type", sa.String(30), nullable=False, index=True),
        sa.Column("status", sa.String(20), nullable=False, server_default="pending", index=True),
        sa.Column("progress", sa.Float, nullable=False, server_default="0.0"),
        sa.Column("current_stage", sa.String(200), nullable=False, server_default=""),
        sa.Column("payload", sa.JSON, nullable=False, server_default="{}"),
        sa.Column("result", sa.JSON, nullable=False, server_default="{}"),
        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("celery_task_id", sa.String(100), nullable=False, server_default=""),
        sa.Column("source_id", sa.String(32), nullable=False, server_default="", index=True),
        sa.Column("created_by_user_id", sa.String(32), nullable=False, server_default="", index=True),
        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("jobs")
