"""Add room members table

Revision ID: 2c5f57dddb78
Revises: 3a5b8d8e53cd
Create Date: 2025-05-23 21:27:17.497481

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect


# revision identifiers, used by Alembic.
revision = '2c5f57dddb78'
down_revision = '3a5b8d8e53cd'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    inspector = inspect(conn)
    tables = inspector.get_table_names()
    
    if 'room_members' not in tables:
        op.create_table('room_members',
        sa.Column('room_id', sa.Integer(), nullable=False),
        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(['room_id'], ['room.id'], ),
        sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
        sa.PrimaryKeyConstraint('room_id', 'user_id')
        )
    
    # Check if is_private column exists before dropping it
    columns = [col['name'] for col in inspector.get_columns('room')]
    if 'is_private' in columns:
        with op.batch_alter_table('room', schema=None) as batch_op:
            batch_op.drop_column('is_private')

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('room', schema=None) as batch_op:
        batch_op.add_column(sa.Column('is_private', sa.BOOLEAN(), autoincrement=False, nullable=True))

    op.drop_table('room_members')
    # ### end Alembic commands ###
