"""add shot replicate tables

Revision ID: dbb11c0b5a0a
Revises: 9ac2212e1b8e
Create Date: 2026-06-11 14:59:26.871902
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = 'dbb11c0b5a0a'
down_revision: Union[str, None] = '9ac2212e1b8e'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('shot_replicate_task_sets',
    sa.Column('id', sa.String(length=32), nullable=False),
    sa.Column('user_id', sa.String(length=32), nullable=False),
    sa.Column('title', sa.String(length=160), nullable=True),
    sa.Column('video_url', sa.String(length=512), nullable=False),
    sa.Column('video_path', sa.String(length=512), nullable=False),
    sa.Column('video_duration_seconds', sa.Float(), nullable=False),
    sa.Column('status', sa.String(length=32), nullable=False),
    sa.Column('analysis_status', sa.String(length=32), nullable=False),
    sa.Column('split_status', sa.String(length=32), nullable=False),
    sa.Column('original_video_content', sa.Text(), nullable=True),
    sa.Column('original_video_category', sa.String(length=160), nullable=True),
    sa.Column('original_video_audience', sa.Text(), nullable=True),
    sa.Column('ai_suggestion_json', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=True),
    sa.Column('analysis_raw_json', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=True),
    sa.Column('analysis_result_json', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=True),
    sa.Column('segment_count', sa.Integer(), nullable=False),
    sa.Column('completed_segment_count', sa.Integer(), nullable=False),
    sa.Column('failed_segment_count', sa.Integer(), nullable=False),
    sa.Column('analysis_error_message', sa.Text(), nullable=True),
    sa.Column('split_error_message', sa.Text(), nullable=True),
    sa.Column('idempotency_key', sa.String(length=64), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_shot_replicate_task_sets_analysis_status', 'shot_replicate_task_sets', ['analysis_status'], unique=False)
    op.create_index('idx_shot_replicate_task_sets_split_status', 'shot_replicate_task_sets', ['split_status'], unique=False)
    op.create_index('idx_shot_replicate_task_sets_status', 'shot_replicate_task_sets', ['status'], unique=False)
    op.create_index('idx_shot_replicate_task_sets_user_created', 'shot_replicate_task_sets', ['user_id', 'created_at'], unique=False)
    op.create_index(op.f('ix_shot_replicate_task_sets_analysis_status'), 'shot_replicate_task_sets', ['analysis_status'], unique=False)
    op.create_index(op.f('ix_shot_replicate_task_sets_deleted_at'), 'shot_replicate_task_sets', ['deleted_at'], unique=False)
    op.create_index(op.f('ix_shot_replicate_task_sets_idempotency_key'), 'shot_replicate_task_sets', ['idempotency_key'], unique=False)
    op.create_index(op.f('ix_shot_replicate_task_sets_split_status'), 'shot_replicate_task_sets', ['split_status'], unique=False)
    op.create_index(op.f('ix_shot_replicate_task_sets_status'), 'shot_replicate_task_sets', ['status'], unique=False)
    op.create_index(op.f('ix_shot_replicate_task_sets_user_id'), 'shot_replicate_task_sets', ['user_id'], unique=False)
    op.create_index('uq_shot_replicate_task_sets_user_idempotency', 'shot_replicate_task_sets', ['user_id', 'idempotency_key'], unique=True, postgresql_where=sa.text('deleted_at IS NULL AND idempotency_key IS NOT NULL'))
    op.create_table('shot_replicate_segments',
    sa.Column('id', sa.String(length=32), nullable=False),
    sa.Column('task_set_id', sa.String(length=32), nullable=False),
    sa.Column('user_id', sa.String(length=32), nullable=False),
    sa.Column('segment_index', sa.Integer(), nullable=False),
    sa.Column('source_mode', sa.String(length=32), nullable=False),
    sa.Column('start_second', sa.Float(), nullable=False),
    sa.Column('end_second', sa.Float(), nullable=False),
    sa.Column('duration_seconds', sa.Float(), nullable=False),
    sa.Column('time_node', sa.String(length=64), nullable=False),
    sa.Column('split_status', sa.String(length=32), nullable=False),
    sa.Column('analysis_status', sa.String(length=32), nullable=False),
    sa.Column('replicate_status', sa.String(length=32), nullable=False),
    sa.Column('segment_video_url', sa.String(length=512), nullable=True),
    sa.Column('segment_video_path', sa.String(length=512), nullable=True),
    sa.Column('original_video_content', sa.Text(), nullable=True),
    sa.Column('original_video_category', sa.String(length=160), nullable=True),
    sa.Column('original_video_audience', sa.Text(), nullable=True),
    sa.Column('segment_content', sa.Text(), nullable=True),
    sa.Column('segment_category', sa.String(length=160), nullable=True),
    sa.Column('segment_audience', sa.Text(), nullable=True),
    sa.Column('analysis_json', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=True),
    sa.Column('ai_suggestion_json', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=True),
    sa.Column('module_project_id', sa.String(length=32), nullable=True),
    sa.Column('split_celery_task_id', sa.String(length=160), nullable=True),
    sa.Column('split_enqueued_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('split_started_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('split_lease_until', sa.DateTime(timezone=True), nullable=True),
    sa.Column('split_next_retry_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('split_retry_count', sa.Integer(), nullable=False),
    sa.Column('split_last_error', sa.Text(), nullable=True),
    sa.Column('split_completed_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('analysis_error_message', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['task_set_id'], ['shot_replicate_task_sets.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_shot_replicate_segments_analysis_status', 'shot_replicate_segments', ['analysis_status'], unique=False)
    op.create_index('idx_shot_replicate_segments_project', 'shot_replicate_segments', ['module_project_id'], unique=False)
    op.create_index('idx_shot_replicate_segments_replicate_status', 'shot_replicate_segments', ['replicate_status'], unique=False)
    op.create_index('idx_shot_replicate_segments_source_mode', 'shot_replicate_segments', ['source_mode'], unique=False)
    op.create_index('idx_shot_replicate_segments_split_status', 'shot_replicate_segments', ['split_status'], unique=False)
    op.create_index('idx_shot_replicate_segments_task_set', 'shot_replicate_segments', ['task_set_id', 'segment_index'], unique=False)
    op.create_index('idx_shot_replicate_segments_user_created', 'shot_replicate_segments', ['user_id', 'created_at'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_analysis_status'), 'shot_replicate_segments', ['analysis_status'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_deleted_at'), 'shot_replicate_segments', ['deleted_at'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_module_project_id'), 'shot_replicate_segments', ['module_project_id'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_replicate_status'), 'shot_replicate_segments', ['replicate_status'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_segment_index'), 'shot_replicate_segments', ['segment_index'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_source_mode'), 'shot_replicate_segments', ['source_mode'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_split_celery_task_id'), 'shot_replicate_segments', ['split_celery_task_id'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_split_lease_until'), 'shot_replicate_segments', ['split_lease_until'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_split_next_retry_at'), 'shot_replicate_segments', ['split_next_retry_at'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_split_status'), 'shot_replicate_segments', ['split_status'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_task_set_id'), 'shot_replicate_segments', ['task_set_id'], unique=False)
    op.create_index(op.f('ix_shot_replicate_segments_user_id'), 'shot_replicate_segments', ['user_id'], unique=False)
    op.create_index('uq_shot_replicate_segments_task_set_index_active', 'shot_replicate_segments', ['task_set_id', 'segment_index'], unique=True, postgresql_where=sa.text('deleted_at IS NULL'))
    op.drop_column('user_oauth_app', 'count')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('user_oauth_app', sa.Column('count', sa.BIGINT(), autoincrement=False, nullable=False, comment='应用最大可以授权多少个用户'))
    op.drop_index('uq_shot_replicate_segments_task_set_index_active', table_name='shot_replicate_segments', postgresql_where=sa.text('deleted_at IS NULL'))
    op.drop_index(op.f('ix_shot_replicate_segments_user_id'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_task_set_id'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_split_status'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_split_next_retry_at'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_split_lease_until'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_split_celery_task_id'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_source_mode'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_segment_index'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_replicate_status'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_module_project_id'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_deleted_at'), table_name='shot_replicate_segments')
    op.drop_index(op.f('ix_shot_replicate_segments_analysis_status'), table_name='shot_replicate_segments')
    op.drop_index('idx_shot_replicate_segments_user_created', table_name='shot_replicate_segments')
    op.drop_index('idx_shot_replicate_segments_task_set', table_name='shot_replicate_segments')
    op.drop_index('idx_shot_replicate_segments_split_status', table_name='shot_replicate_segments')
    op.drop_index('idx_shot_replicate_segments_source_mode', table_name='shot_replicate_segments')
    op.drop_index('idx_shot_replicate_segments_replicate_status', table_name='shot_replicate_segments')
    op.drop_index('idx_shot_replicate_segments_project', table_name='shot_replicate_segments')
    op.drop_index('idx_shot_replicate_segments_analysis_status', table_name='shot_replicate_segments')
    op.drop_table('shot_replicate_segments')
    op.drop_index('uq_shot_replicate_task_sets_user_idempotency', table_name='shot_replicate_task_sets', postgresql_where=sa.text('deleted_at IS NULL AND idempotency_key IS NOT NULL'))
    op.drop_index(op.f('ix_shot_replicate_task_sets_user_id'), table_name='shot_replicate_task_sets')
    op.drop_index(op.f('ix_shot_replicate_task_sets_status'), table_name='shot_replicate_task_sets')
    op.drop_index(op.f('ix_shot_replicate_task_sets_split_status'), table_name='shot_replicate_task_sets')
    op.drop_index(op.f('ix_shot_replicate_task_sets_idempotency_key'), table_name='shot_replicate_task_sets')
    op.drop_index(op.f('ix_shot_replicate_task_sets_deleted_at'), table_name='shot_replicate_task_sets')
    op.drop_index(op.f('ix_shot_replicate_task_sets_analysis_status'), table_name='shot_replicate_task_sets')
    op.drop_index('idx_shot_replicate_task_sets_user_created', table_name='shot_replicate_task_sets')
    op.drop_index('idx_shot_replicate_task_sets_status', table_name='shot_replicate_task_sets')
    op.drop_index('idx_shot_replicate_task_sets_split_status', table_name='shot_replicate_task_sets')
    op.drop_index('idx_shot_replicate_task_sets_analysis_status', table_name='shot_replicate_task_sets')
    op.drop_table('shot_replicate_task_sets')
    # ### end Alembic commands ###
