"""rename national_dex to pokeapi_id, add real national_dex

Revision ID: e5f6a7b8c9d0
Revises: d4e5f6a7b8c9
Create Date: 2026-02-07 10:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    # Rename national_dex -> pokeapi_id and widen to Integer
    op.alter_column(
        'pokemon', 'national_dex',
        new_column_name='pokeapi_id',
        type_=sa.Integer(),
        existing_type=sa.SmallInteger(),
        existing_nullable=False,
    )

    # Add real national_dex column (shared between forms and base species)
    op.add_column(
        'pokemon',
        sa.Column('national_dex', sa.SmallInteger(), nullable=False, server_default='0'),
    )

    # Populate national_dex = pokeapi_id for all existing rows
    # (correct for base species; forms will be fixed by re-seeding)
    op.execute('UPDATE pokemon SET national_dex = pokeapi_id')

    # Remove the default now that all rows are populated
    op.alter_column('pokemon', 'national_dex', server_default=None)


def downgrade() -> None:
    op.drop_column('pokemon', 'national_dex')
    op.alter_column(
        'pokemon', 'pokeapi_id',
        new_column_name='national_dex',
        type_=sa.SmallInteger(),
        existing_type=sa.Integer(),
        existing_nullable=False,
    )
