"""add module generation project and hot opening replicate

Revision ID: 150fc6da855f
Revises: 476b259992de
Create Date: 2026-06-10 11:56:22.146017
"""
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 = '150fc6da855f'
down_revision: Union[str, None] = '476b259992de'
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('module_generation_projects',
    sa.Column('id', sa.String(length=32), nullable=False),
    sa.Column('user_id', sa.String(length=32), nullable=False),
    sa.Column('module', sa.String(length=64), nullable=False),
    sa.Column('title', sa.String(length=160), nullable=True),
    sa.Column('status', sa.String(length=32), nullable=False),
    sa.Column('current_step_code', sa.String(length=64), nullable=True),
    sa.Column('final_image_url', sa.String(length=512), nullable=True),
    sa.Column('final_video_url', sa.String(length=512), nullable=True),
    sa.Column('final_video_cover_url', sa.String(length=512), nullable=True),
    sa.Column('error_message', sa.Text(), nullable=True),
    sa.Column('idempotency_key', sa.String(length=64), nullable=True),
    sa.Column('completed_at', sa.DateTime(timezone=True), 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_module_generation_projects_status', 'module_generation_projects', ['module', 'status'], unique=False)
    op.create_index('idx_module_generation_projects_user_module', 'module_generation_projects', ['user_id', 'module'], unique=False)
    op.create_index(op.f('ix_module_generation_projects_current_step_code'), 'module_generation_projects', ['current_step_code'], unique=False)
    op.create_index(op.f('ix_module_generation_projects_deleted_at'), 'module_generation_projects', ['deleted_at'], unique=False)
    op.create_index(op.f('ix_module_generation_projects_idempotency_key'), 'module_generation_projects', ['idempotency_key'], unique=False)
    op.create_index(op.f('ix_module_generation_projects_module'), 'module_generation_projects', ['module'], unique=False)
    op.create_index(op.f('ix_module_generation_projects_status'), 'module_generation_projects', ['status'], unique=False)
    op.create_index(op.f('ix_module_generation_projects_user_id'), 'module_generation_projects', ['user_id'], unique=False)
    op.create_index('uq_module_generation_projects_user_module_idempotency', 'module_generation_projects', ['user_id', 'module', 'idempotency_key'], unique=True, postgresql_where=sa.text('deleted_at IS NULL AND idempotency_key IS NOT NULL'))
    op.create_table('module_generation_steps',
    sa.Column('id', sa.String(length=32), nullable=False),
    sa.Column('project_id', sa.String(length=32), nullable=False),
    sa.Column('user_id', sa.String(length=32), nullable=False),
    sa.Column('module', sa.String(length=64), nullable=False),
    sa.Column('step_index', sa.Integer(), nullable=False),
    sa.Column('step_code', sa.String(length=64), nullable=False),
    sa.Column('status', sa.String(length=32), nullable=False),
    sa.Column('version', sa.Integer(), nullable=False),
    sa.Column('is_current', sa.Boolean(), nullable=False),
    sa.Column('parent_step_id', sa.String(length=32), nullable=True),
    sa.Column('source_step_id', sa.String(length=32), nullable=True),
    sa.Column('chat_task_id', sa.String(length=32), nullable=True),
    sa.Column('input_json', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=True),
    sa.Column('output_json', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), nullable=True),
    sa.Column('error_message', sa.Text(), nullable=True),
    sa.Column('started_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('completed_at', sa.DateTime(timezone=True), 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(['chat_task_id'], ['chat_generation_tasks.id'], ondelete='SET NULL'),
    sa.ForeignKeyConstraint(['project_id'], ['module_generation_projects.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_module_generation_steps_chat_task', 'module_generation_steps', ['chat_task_id'], unique=False)
    op.create_index('idx_module_generation_steps_project_code', 'module_generation_steps', ['project_id', 'step_code', 'is_current'], unique=False)
    op.create_index('idx_module_generation_steps_project_current', 'module_generation_steps', ['project_id', 'is_current', 'deleted_at'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_chat_task_id'), 'module_generation_steps', ['chat_task_id'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_deleted_at'), 'module_generation_steps', ['deleted_at'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_is_current'), 'module_generation_steps', ['is_current'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_module'), 'module_generation_steps', ['module'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_parent_step_id'), 'module_generation_steps', ['parent_step_id'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_project_id'), 'module_generation_steps', ['project_id'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_source_step_id'), 'module_generation_steps', ['source_step_id'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_status'), 'module_generation_steps', ['status'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_step_code'), 'module_generation_steps', ['step_code'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_step_index'), 'module_generation_steps', ['step_index'], unique=False)
    op.create_index(op.f('ix_module_generation_steps_user_id'), 'module_generation_steps', ['user_id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_module_generation_steps_user_id'), table_name='module_generation_steps')
    op.drop_index(op.f('ix_module_generation_steps_step_index'), table_name='module_generation_steps')
    op.drop_index(op.f('ix_module_generation_steps_step_code'), table_name='module_generation_steps')
    op.drop_index(op.f('ix_module_generation_steps_status'), table_name='module_generation_steps')
    op.drop_index(op.f('ix_module_generation_steps_source_step_id'), table_name='module_generation_steps')
    op.drop_index(op.f('ix_module_generation_steps_project_id'), table_name='module_generation_steps')
    op.drop_index(op.f('ix_module_generation_steps_parent_step_id'), table_name='module_generation_steps')
    op.drop_index(op.f('ix_module_generation_steps_module'), table_name='module_generation_steps')
    op.drop_index(op.f('ix_module_generation_steps_is_current'), table_name='module_generation_steps')
    op.drop_index(op.f('ix_module_generation_steps_deleted_at'), table_name='module_generation_steps')
    op.drop_index(op.f('ix_module_generation_steps_chat_task_id'), table_name='module_generation_steps')
    op.drop_index('idx_module_generation_steps_project_current', table_name='module_generation_steps')
    op.drop_index('idx_module_generation_steps_project_code', table_name='module_generation_steps')
    op.drop_index('idx_module_generation_steps_chat_task', table_name='module_generation_steps')
    op.drop_table('module_generation_steps')
    op.drop_index('uq_module_generation_projects_user_module_idempotency', table_name='module_generation_projects', postgresql_where=sa.text('deleted_at IS NULL AND idempotency_key IS NOT NULL'))
    op.drop_index(op.f('ix_module_generation_projects_user_id'), table_name='module_generation_projects')
    op.drop_index(op.f('ix_module_generation_projects_status'), table_name='module_generation_projects')
    op.drop_index(op.f('ix_module_generation_projects_module'), table_name='module_generation_projects')
    op.drop_index(op.f('ix_module_generation_projects_idempotency_key'), table_name='module_generation_projects')
    op.drop_index(op.f('ix_module_generation_projects_deleted_at'), table_name='module_generation_projects')
    op.drop_index(op.f('ix_module_generation_projects_current_step_code'), table_name='module_generation_projects')
    op.drop_index('idx_module_generation_projects_user_module', table_name='module_generation_projects')
    op.drop_index('idx_module_generation_projects_status', table_name='module_generation_projects')
    op.drop_table('module_generation_projects')
    # ### end Alembic commands ###
