"""v0.2.0 migration

Revision ID: 0ab200a7f196
Revises: 5fd61bc55e09
Create Date: 2024-05-24 13:39:50.917676

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.engine.reflection import Inspector

# revision identifiers, used by Alembic.
revision: str = '0ab200a7f196'
down_revision: Union[str, None] = '1bce2bd27873'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Get database connection and inspect constraints
    conn = op.get_bind()
    inspector = Inspector.from_engine(conn)

    # Drop foreign key constraint dynamically
    constraints_access_tokens = inspector.get_foreign_keys('access_tokens')
    for constraint in constraints_access_tokens:
        op.drop_constraint(constraint['name'], 'access_tokens', type_='foreignkey')

    constraints_followers = inspector.get_foreign_keys('followers')
    for constraint in constraints_followers:
        op.drop_constraint(constraint['name'], 'followers', type_='foreignkey')
    # ### commands auto generated by Alembic - please adjust! ###
    # Then drop the index
    op.drop_index('ix_access_tokens_user_id', table_name='access_tokens')
    op.drop_table('access_tokens')
    op.alter_column('users_integrations', 'user_id',
               existing_type=sa.INTEGER(),
               comment='User ID that the integration belongs',
               existing_comment='User ID that the token belongs',
               existing_nullable=False)
    op.create_foreign_key(None, 'followers', 'users', ['following_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'followers', 'users', ['follower_id'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade() -> None:
    # Drop the CASCADE constraints
    conn = op.get_bind()
    inspector = Inspector.from_engine(conn)

    constraints_followers = inspector.get_foreign_keys('followers')
    for constraint in constraints_followers:
        op.drop_constraint(constraint['name'], 'followers', type_='foreignkey')
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('users_integrations', 'user_id',
               existing_type=sa.INTEGER(),
               comment='User ID that the token belongs',
               existing_comment='User ID that the integration belongs',
               existing_nullable=False)
    op.create_table('access_tokens',
    sa.Column('id', sa.INTEGER(display_width=11), autoincrement=True, nullable=False),
    sa.Column('token', sa.VARCHAR(length=256), nullable=False, comment='User token'),
    sa.Column('user_id', sa.INTEGER(display_width=11), autoincrement=False, nullable=False, comment='User ID that the token belongs'),
    sa.Column('created_at', sa.DATETIME(), nullable=False, comment='Token creation date (date)'),
    sa.Column('expires_at', sa.DATETIME(), nullable=False, comment='Token expiration date (date)'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], name='access_tokens_ibfk_1', ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa_collate='utf8mb4_general_ci',
    sa_default_charset='utf8mb4',
    sa_engine='InnoDB'
    )
    # Recreate the index first
    op.create_index('ix_access_tokens_user_id', 'access_tokens', ['user_id'])
    # Then recreate the foreign key constraint
    op.create_foreign_key('access_tokens_ibfk_1', 'access_tokens', 'users', ['user_id'], ['id'])

    # Recreate foreign keys without CASCADE
    op.create_foreign_key('followers_ibfk_1', 'followers', 'users', ['follower_id'], ['id'])
    op.create_foreign_key('followers_ibfk_2', 'followers', 'users', ['following_id'], ['id'])
    # ### end Alembic commands ###
