"""add uuid and soft delete

Revision ID: a24538bfa409
Revises: b63374a17449
Create Date: 2025-06-27 12:27:12.999523
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'a24538bfa409'
down_revision: Union[str, None] = 'b63374a17449'
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.add_column('countries', sa.Column('uuid', sa.UUID(), nullable=False))
    op.add_column('countries', sa.Column('is_deleted', sa.Boolean(), nullable=False))
    op.add_column('countries', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_index(op.f('ix_countries_uuid'), 'countries', ['uuid'], unique=True)

    op.add_column('regions', sa.Column('uuid', sa.UUID(), nullable=False))
    op.add_column('regions', sa.Column('is_deleted', sa.Boolean(), nullable=False))
    op.add_column('regions', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_index(op.f('ix_regions_uuid'), 'regions', ['uuid'], unique=True)

    op.add_column('locations', sa.Column('uuid', sa.UUID(), nullable=False))
    op.add_column('locations', sa.Column('is_deleted', sa.Boolean(), nullable=False))
    op.add_column('locations', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_index(op.f('ix_locations_uuid'), 'locations', ['uuid'], unique=True)

    op.add_column('sub_regions', sa.Column('uuid', sa.UUID(), nullable=False))
    op.add_column('sub_regions', sa.Column('is_deleted', sa.Boolean(), nullable=False))
    op.add_column('sub_regions', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_index(op.f('ix_sub_regions_uuid'), 'sub_regions', ['uuid'], unique=True)

    # Drop old FK from locales before modifying
    op.drop_constraint(op.f('locales_location_id_fkey'), 'locales', type_='foreignkey')
    op.drop_column('locales', 'location_id')

    op.add_column('locales', sa.Column('uuid', sa.UUID(), nullable=False))
    op.add_column('locales', sa.Column('location_uuid', sa.UUID(), nullable=False))
    op.add_column('locales', sa.Column('is_deleted', sa.Boolean(), nullable=False))
    op.add_column('locales', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_index(op.f('ix_locales_uuid'), 'locales', ['uuid'], unique=True)
    op.create_foreign_key(None, 'locales', 'locations', ['location_uuid'], ['uuid'], ondelete='CASCADE')

    # Fix web_crawlers FK order (already uses integer `id` references)
    op.drop_index(op.f('ix_web_crawlers_country_id'), table_name='web_crawlers')
    op.drop_index(op.f('ix_web_crawlers_location_id'), table_name='web_crawlers')
    op.drop_constraint(op.f('web_crawlers_country_id_fkey'), 'web_crawlers', type_='foreignkey')
    op.drop_constraint(op.f('web_crawlers_location_id_fkey'), 'web_crawlers', type_='foreignkey')
    op.create_foreign_key(None, 'web_crawlers', 'countries', ['country_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key(None, 'web_crawlers', 'locations', ['location_id'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'web_crawlers', type_='foreignkey')
    op.drop_constraint(None, 'web_crawlers', type_='foreignkey')
    op.create_foreign_key(op.f('web_crawlers_location_id_fkey'), 'web_crawlers', 'locations', ['location_id'], ['id'])
    op.create_foreign_key(op.f('web_crawlers_country_id_fkey'), 'web_crawlers', 'countries', ['country_id'], ['id'])
    op.create_index(op.f('ix_web_crawlers_location_id'), 'web_crawlers', ['location_id'], unique=False)
    op.create_index(op.f('ix_web_crawlers_country_id'), 'web_crawlers', ['country_id'], unique=False)

    # Drop locales new FK/fields and restore old
    op.drop_constraint(None, 'locales', type_='foreignkey')
    op.drop_index(op.f('ix_locales_uuid'), table_name='locales')
    op.drop_column('locales', 'deleted_at')
    op.drop_column('locales', 'is_deleted')
    op.drop_column('locales', 'location_uuid')
    op.drop_column('locales', 'uuid')
    op.add_column('locales', sa.Column('location_id', sa.INTEGER(), nullable=False))
    op.create_foreign_key(op.f('locales_location_id_fkey'), 'locales', 'locations', ['location_id'], ['id'])

    # Drop other UUID and soft-delete columns
    op.drop_index(op.f('ix_sub_regions_uuid'), table_name='sub_regions')
    op.drop_column('sub_regions', 'deleted_at')
    op.drop_column('sub_regions', 'is_deleted')
    op.drop_column('sub_regions', 'uuid')

    op.drop_index(op.f('ix_regions_uuid'), table_name='regions')
    op.drop_column('regions', 'deleted_at')
    op.drop_column('regions', 'is_deleted')
    op.drop_column('regions', 'uuid')

    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_locations_uuid'), table_name='locations')
    op.drop_column('locations', 'deleted_at')
    op.drop_column('locations', 'is_deleted')
    op.drop_column('locations', 'uuid')

    op.drop_index(op.f('ix_countries_uuid'), table_name='countries')
    op.drop_column('countries', 'deleted_at')
    op.drop_column('countries', 'is_deleted')
    op.drop_column('countries', 'uuid')
    # ### end Alembic commands ###
