"""Add soft delete and update unique constraints to include deleted_at

Revision ID: soft_delete_all
Revises: 
Create Date: 2025-11-08 00:38:03

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '20251108_soft_delete_all'
down_revision = 'remove_product_slug_20251107'
branch_labels = None
depends_on = None

def upgrade() -> None:
    op.add_column('product_likes', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_unique_constraint('uq_product_likes_product_user', 'product_likes', ['product_id', 'user_id', 'deleted_at'])
    
    # Drop the old unique index
    op.drop_index('ix_product_likes_user_product', table_name='product_likes')

    # Create a new unique index that includes deleted_at
    op.create_index(
        'ix_product_likes_user_product',
        'product_likes',
        ['user_id', 'product_id', 'deleted_at'],
        unique=True
    )


    op.add_column('product_allergens', sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False))
    op.add_column('product_allergens', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.drop_constraint('uq_product_allergens_product_name', 'product_allergens', type_='unique')
    op.create_unique_constraint('uq_product_allergens_product_name', 'product_allergens', ['product_id', 'name', 'deleted_at'])
    
    
    op.add_column('product_attributes', sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False))
    op.add_column('product_attributes', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.drop_constraint('uq_product_attributes_product_key', 'product_attributes', type_='unique')
    op.create_unique_constraint('uq_product_attributes_product_key', 'product_attributes', ['product_id', 'key', 'deleted_at'])


    op.add_column('product_images', sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False))
    op.add_column('product_images', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_unique_constraint('uq_product_images', 'product_images', ['product_id', 'position', 'deleted_at'])
    
    
    
    op.add_column('product_labels', sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False))
    op.add_column('product_labels', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_unique_constraint('uq_product_labels', 'product_labels', ['product_id', 'name', 'deleted_at'])
    
    
    
    op.add_column('product_nutrition', sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False))
    op.add_column('product_nutrition', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_unique_constraint('uq_product_nutrition', 'product_nutrition', ['product_id', 'key', 'deleted_at'])
    
    
    op.add_column('product_sections', sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False))
    op.add_column('product_sections', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_unique_constraint('uq_product_sections', 'product_sections', ['product_id', 'title', 'deleted_at'])


    op.add_column('product_stickers', sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False))
    op.add_column('product_stickers', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_unique_constraint('uq_product_stickers', 'product_stickers', ['product_id', 'name', 'deleted_at'])


    op.add_column('nav_tops', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_unique_constraint('uq_nav_tops', 'nav_tops', ['slug', 'deleted_at'])

    
    
    op.add_column('nav_subs', sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False))
    op.add_column('nav_subs', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_unique_constraint('uq_nav_subs', 'nav_subs', ['top_id', 'slug', 'deleted_at'])

    
    
    op.add_column('listings', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.drop_constraint('uq_listings_top_sub', 'listings', type_='unique')
    op.create_unique_constraint('uq_listings_top_sub', 'listings', ['top_id', 'sub_id', 'deleted_at'])
    
    op.add_column('listing_items', sa.Column('created_at', sa.DateTime(), server_default=sa.func.now(), nullable=False))
    op.add_column('listing_items', sa.Column('deleted_at', sa.DateTime(), nullable=True))
    op.create_unique_constraint('uq_listing_items', 'listing_items', ['listing_id', 'slug', 'deleted_at'])

def downgrade() -> None:
    

    # Drop the modified index
    op.drop_index('ix_product_likes_user_product', table_name='product_likes')

    # Recreate the old unique index
    op.create_index(
        'ix_product_likes_user_product',
        'product_likes',
        ['user_id', 'product_id'],
        unique=True
    )
    op.drop_constraint('uq_product_likes_product_user', 'product_likes', type_='unique')
    op.drop_column('product_likes', 'deleted_at')
    


    op.drop_constraint('uq_product_allergens_product_name', 'product_allergens', type_='unique')
    op.drop_column('product_allergens', 'deleted_at')
    op.drop_column('product_allergens', 'created_at')
    op.create_unique_constraint('uq_product_allergens_product_name', 'product_allergens', ['product_id', 'name'])

    op.drop_constraint('uq_product_attributes_product_key', 'product_attributes', type_='unique')
    op.drop_column('product_attributes', 'deleted_at')
    op.drop_column('product_attributes', 'created_at')
    op.create_unique_constraint('uq_product_attributes_product_key', 'product_attributes', ['product_id', 'key'])
    
    
    op.drop_constraint('uq_product_images', 'product_images', type_='unique')
    op.drop_column('product_images', 'deleted_at')
    op.drop_column('product_images', 'created_at')
    

    op.drop_constraint('uq_product_labels', 'product_labels', type_='unique')
    op.drop_column('product_labels', 'deleted_at')
    op.drop_column('product_labels', 'created_at')
    
    
    
    op.drop_constraint('uq_product_nutrition', 'product_nutrition', type_='unique')
    op.drop_column('product_nutrition', 'deleted_at')
    op.drop_column('product_nutrition', 'created_at')
    


    op.drop_constraint('uq_product_sections', 'product_sections', type_='unique')
    op.drop_column('product_sections', 'deleted_at')
    op.drop_column('product_sections', 'created_at')
    
    
    op.drop_constraint('uq_product_stickers', 'product_stickers', type_='unique')
    op.drop_column('product_stickers', 'deleted_at')
    op.drop_column('product_stickers', 'created_at')
    
    
    
    op.drop_constraint('uq_nav_tops', 'nav_tops', type_='unique')
    op.drop_column('nav_tops', 'deleted_at')
    

    op.drop_constraint('uq_nav_subs', 'nav_subs', type_='unique')
    op.drop_column('nav_subs', 'deleted_at')
    op.drop_column('nav_subs', 'created_at')
    
    
    op.drop_constraint('uq_listings_top_sub', 'listings', type_='unique')
    op.drop_column('listings', 'deleted_at')
    op.create_unique_constraint('uq_listings_top_sub', 'listings', ['top_id', 'sub_id'])
    
    op.drop_constraint('uq_listing_items', 'listing_items', type_='unique')
    op.drop_column('listing_items', 'deleted_at')
    op.drop_column('listing_items', 'created_at')
    