"""add room_file table for file/folder metadata and uploader info

Revision ID: 64b5c28510b0
Revises: b978642e7b10
Create Date: 2025-05-24 10:07:02.159730

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


# revision identifiers, used by Alembic.
revision = '64b5c28510b0'
down_revision = 'b978642e7b10'
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_file' not in tables:
        op.create_table('room_file',
            sa.Column('id', sa.Integer(), nullable=False),
            sa.Column('room_id', sa.Integer(), nullable=False),
            sa.Column('name', sa.String(length=255), nullable=False),
            sa.Column('path', sa.String(length=1024), nullable=False),
            sa.Column('type', sa.String(length=10), nullable=False),
            sa.Column('size', sa.BigInteger(), nullable=True),
            sa.Column('modified', sa.Float(), nullable=True),
            sa.Column('uploaded_by', sa.Integer(), nullable=False),
            sa.Column('uploaded_at', sa.DateTime(), nullable=False),
            sa.ForeignKeyConstraint(['room_id'], ['room.id'], ),
            sa.ForeignKeyConstraint(['uploaded_by'], ['user.id'], ),
            sa.PrimaryKeyConstraint('id')
        )
    
    # Check if preferred_view column exists before trying to drop it
    columns = [col['name'] for col in inspector.get_columns('room_member_permissions')]
    if 'preferred_view' in columns:
        with op.batch_alter_table('room_member_permissions', schema=None) as batch_op:
            batch_op.drop_column('preferred_view')

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('room_member_permissions', schema=None) as batch_op:
        batch_op.add_column(sa.Column('preferred_view', sa.VARCHAR(length=10), autoincrement=False, nullable=False))

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