"""add file module and file_id to WebCrawler

Revision ID: 3b72170cce99
Revises: a24538bfa409
Create Date: 2025-06-30 19:44:31.532299

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('files',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('original_name', sa.String(length=255), nullable=False, comment='Original file name before upload/rename'),
    sa.Column('name', sa.String(length=255), nullable=False, comment='Stored file name (after renaming, if any)'),
    sa.Column('path', sa.String(length=255), nullable=False, comment='File path in storage'),
    sa.Column('mime', sa.String(length=100), nullable=True, comment='MIME type of the file'),
    sa.Column('created_at', sa.DateTime(), nullable=False, comment='Upload timestamp'),
    sa.Column('created_by_id', sa.Integer(), nullable=False, comment="Uploader's user ID"),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_files_id'), 'files', ['id'], unique=False)
    op.drop_constraint(op.f('locations_region_id_fkey'), 'locations', type_='foreignkey')
    op.create_foreign_key(None, 'locations', 'regions', ['region_id'], ['id'], ondelete='CASCADE')
    op.add_column('web_crawlers', sa.Column('bottle_size_default', sa.String(length=50), nullable=True))
    op.add_column('web_crawlers', sa.Column('bottle_size_index', sa.String(length=50), nullable=True))
    op.add_column('web_crawlers', sa.Column('file_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'web_crawlers', 'files', ['file_id'], ['id'], ondelete='SET NULL')
    op.drop_column('web_crawlers', 'button_size_default')
    op.drop_column('web_crawlers', 'button_size_index')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('web_crawlers', sa.Column('button_size_index', sa.VARCHAR(length=50), autoincrement=False, nullable=True))
    op.add_column('web_crawlers', sa.Column('button_size_default', sa.VARCHAR(length=50), autoincrement=False, nullable=True))
    op.drop_constraint(None, 'web_crawlers', type_='foreignkey')
    op.drop_column('web_crawlers', 'file_id')
    op.drop_column('web_crawlers', 'bottle_size_index')
    op.drop_column('web_crawlers', 'bottle_size_default')
    op.drop_constraint(None, 'locations', type_='foreignkey')
    op.create_foreign_key(op.f('locations_region_id_fkey'), 'locations', 'regions', ['region_id'], ['id'])
    op.drop_index(op.f('ix_files_id'), table_name='files')
    op.drop_table('files')
    # ### end Alembic commands ###
