"""create_user_sessions_table

Revision ID: 54e4d0db31a3
Revises: d709b14aa24a
Create Date: 2025-12-06 01:12:15.123456

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql


# revision identifiers, used by Alembic.
revision = '54e4d0db31a3'
down_revision = 'd709b14aa24a'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Check if table exists before creating
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    tables = inspector.get_table_names()
    
    if 'user_sessions' not in tables:
        # Create user sessions table
        op.create_table(
            'user_sessions',
            sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
            sa.Column('user_id', sa.Integer(), nullable=False),
            sa.Column('session_token', sa.String(length=255), nullable=False),
            sa.Column('refresh_token', sa.String(length=255), nullable=True),
            sa.Column('ip_address', sa.String(length=45), nullable=True),
            sa.Column('user_agent', sa.String(length=500), nullable=True),
            sa.Column('device_info', sa.Text(), nullable=True),
            sa.Column('is_active', sa.Boolean(), nullable=False, server_default='1'),
            sa.Column('last_activity', sa.DateTime(), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')),
            sa.Column('expires_at', sa.DateTime(), nullable=False),
            sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')),
            sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
            sa.PrimaryKeyConstraint('id')
        )
        
        # Create indexes
        op.create_index(op.f('ix_user_sessions_id'), 'user_sessions', ['id'], unique=False)
        op.create_index(op.f('ix_user_sessions_user_id'), 'user_sessions', ['user_id'], unique=False)
        op.create_index(op.f('ix_user_sessions_session_token'), 'user_sessions', ['session_token'], unique=True)
        op.create_index(op.f('ix_user_sessions_refresh_token'), 'user_sessions', ['refresh_token'], unique=True)
        op.create_index(op.f('ix_user_sessions_is_active'), 'user_sessions', ['is_active'], unique=False)
        op.create_index(op.f('ix_user_sessions_last_activity'), 'user_sessions', ['last_activity'], unique=False)
        op.create_index(op.f('ix_user_sessions_expires_at'), 'user_sessions', ['expires_at'], unique=False)


def downgrade() -> None:
    # Drop indexes
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    tables = inspector.get_table_names()
    
    if 'user_sessions' in tables:
        try:
            op.drop_index(op.f('ix_user_sessions_expires_at'), table_name='user_sessions')
        except Exception:
            pass
        try:
            op.drop_index(op.f('ix_user_sessions_last_activity'), table_name='user_sessions')
        except Exception:
            pass
        try:
            op.drop_index(op.f('ix_user_sessions_is_active'), table_name='user_sessions')
        except Exception:
            pass
        try:
            op.drop_index(op.f('ix_user_sessions_refresh_token'), table_name='user_sessions')
        except Exception:
            pass
        try:
            op.drop_index(op.f('ix_user_sessions_session_token'), table_name='user_sessions')
        except Exception:
            pass
        try:
            op.drop_index(op.f('ix_user_sessions_user_id'), table_name='user_sessions')
        except Exception:
            pass
        try:
            op.drop_index(op.f('ix_user_sessions_id'), table_name='user_sessions')
        except Exception:
            pass
        
        # Drop table
        op.drop_table('user_sessions')
