"""add file management tables

Revision ID: df320ecde84f
Revises: 15c107310eea
Create Date: 2026-07-15 02:58:18.116905

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel


# revision identifiers, used by Alembic.
revision: str = 'df320ecde84f'
down_revision: Union[str, Sequence[str], None] = '15c107310eea'
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('file_categories',
    sa.Column('id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('parent_id', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['parent_id'], ['file_categories.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('file_records',
    sa.Column('id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('original_name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('storage_name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('category_id', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('file_size', sa.Integer(), nullable=False),
    sa.Column('mime_type', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('file_ext', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['category_id'], ['file_categories.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('eval_results', schema=None) as batch_op:
        batch_op.alter_column('run_id',
               existing_type=sa.VARCHAR(),
               nullable=True)

    with op.batch_alter_table('eval_runs', schema=None) as batch_op:
        batch_op.alter_column('target_id',
               existing_type=sa.VARCHAR(),
               nullable=True)
        batch_op.alter_column('scenario_id',
               existing_type=sa.VARCHAR(),
               nullable=True)

    with op.batch_alter_table('turns', schema=None) as batch_op:
        batch_op.alter_column('run_id',
               existing_type=sa.VARCHAR(),
               nullable=True)

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('turns', schema=None) as batch_op:
        batch_op.alter_column('run_id',
               existing_type=sa.VARCHAR(),
               nullable=False)

    with op.batch_alter_table('eval_runs', schema=None) as batch_op:
        batch_op.alter_column('scenario_id',
               existing_type=sa.VARCHAR(),
               nullable=False)
        batch_op.alter_column('target_id',
               existing_type=sa.VARCHAR(),
               nullable=False)

    with op.batch_alter_table('eval_results', schema=None) as batch_op:
        batch_op.alter_column('run_id',
               existing_type=sa.VARCHAR(),
               nullable=False)

    op.drop_table('file_records')
    op.drop_table('file_categories')
    # ### end Alembic commands ###
