"""Add indexes and database optimizations

Revision ID: add_indexes_opt
Revises: bfac9b8e32f5
Create Date: 2026-01-14 10:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """Add indexes, soft delete columns, and full-text search support."""
    
    # Add soft delete columns
    op.add_column('projects', sa.Column('deleted_at', sa.Float(), nullable=True))
    op.add_column('assets', sa.Column('deleted_at', sa.Float(), nullable=True))
    op.add_column('episodes', sa.Column('deleted_at', sa.Float(), nullable=True))
    op.add_column('storyboards', sa.Column('deleted_at', sa.Float(), nullable=True))
    op.add_column('tasks', sa.Column('deleted_at', sa.Float(), nullable=True))
    
    # Add indexes for frequently queried fields on projects
    op.create_index('idx_projects_created_at', 'projects', ['created_at'])
    op.create_index('idx_projects_updated_at', 'projects', ['updated_at'])
    op.create_index('idx_projects_status', 'projects', ['status'])
    op.create_index('idx_projects_deleted_at', 'projects', ['deleted_at'])
    
    # Add indexes for tasks
    op.create_index('idx_tasks_status', 'tasks', ['status'])
    op.create_index('idx_tasks_type', 'tasks', ['type'])
    op.create_index('idx_tasks_created_at', 'tasks', ['created_at'])
    op.create_index('idx_tasks_type_status', 'tasks', ['type', 'status'])
    op.create_index('idx_tasks_deleted_at', 'tasks', ['deleted_at'])
    
    # Add indexes for assets
    op.create_index('idx_assets_type', 'assets', ['type'])
    op.create_index('idx_assets_deleted_at', 'assets', ['deleted_at'])
    
    # Add indexes for episodes
    op.create_index('idx_episodes_status', 'episodes', ['status'])
    op.create_index('idx_episodes_order_index', 'episodes', ['order_index'])
    op.create_index('idx_episodes_deleted_at', 'episodes', ['deleted_at'])
    
    # Add indexes for storyboards
    op.create_index('idx_storyboards_type', 'storyboards', ['type'])
    op.create_index('idx_storyboards_order_index', 'storyboards', ['order_index'])
    op.create_index('idx_storyboards_deleted_at', 'storyboards', ['deleted_at'])
    
    # Note: SQLite doesn't support full-text search indexes like PostgreSQL
    # For SQLite, we'll use the FTS5 virtual table approach in the application layer
    # or use LIKE queries with indexes on the name columns
    # Adding index on name columns for better LIKE query performance
    op.create_index('idx_projects_name', 'projects', ['name'])
    op.create_index('idx_assets_name', 'assets', ['name'])


def downgrade() -> None:
    """Remove indexes, soft delete columns, and full-text search support."""
    
    # Drop indexes
    op.drop_index('idx_assets_name', table_name='assets')
    op.drop_index('idx_projects_name', table_name='projects')
    
    op.drop_index('idx_storyboards_deleted_at', table_name='storyboards')
    op.drop_index('idx_storyboards_order_index', table_name='storyboards')
    op.drop_index('idx_storyboards_type', table_name='storyboards')
    
    op.drop_index('idx_episodes_deleted_at', table_name='episodes')
    op.drop_index('idx_episodes_order_index', table_name='episodes')
    op.drop_index('idx_episodes_status', table_name='episodes')
    
    op.drop_index('idx_assets_deleted_at', table_name='assets')
    op.drop_index('idx_assets_type', table_name='assets')
    
    op.drop_index('idx_tasks_deleted_at', table_name='tasks')
    op.drop_index('idx_tasks_type_status', table_name='tasks')
    op.drop_index('idx_tasks_created_at', table_name='tasks')
    op.drop_index('idx_tasks_type', table_name='tasks')
    op.drop_index('idx_tasks_status', table_name='tasks')
    
    op.drop_index('idx_projects_deleted_at', table_name='projects')
    op.drop_index('idx_projects_status', table_name='projects')
    op.drop_index('idx_projects_updated_at', table_name='projects')
    op.drop_index('idx_projects_created_at', table_name='projects')
    
    # Drop soft delete columns
    op.drop_column('tasks', 'deleted_at')
    op.drop_column('storyboards', 'deleted_at')
    op.drop_column('episodes', 'deleted_at')
    op.drop_column('assets', 'deleted_at')
    op.drop_column('projects', 'deleted_at')
