"""Update nav_tops unique constraint to include deleted_at"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision='20251108_1_remove extra_uqs'
down_revision = '20251108_nav_tops_soft_delete'
branch_labels = None
depends_on = None


def upgrade():
    
    # Drop existing constraint
    op.drop_constraint('uq_product_images', 'product_images', type_='unique')
    op.drop_constraint('uq_product_images_product_url_kind', 'product_images', type_='unique')
    op.create_unique_constraint("uq_product_images_product_url_kind", "product_images", ["product_id", "url", "kind", "deleted_at"])
    
    op.drop_constraint('uq_product_labels', 'product_labels', type_='unique')
    op.drop_constraint('uq_product_labels_product_name', 'product_labels', type_='unique')
    op.create_unique_constraint("uq_product_labels_product_name", "product_labels", ["product_id", "name", "deleted_at"])
    
    op.drop_constraint('uq_product_nutrition', 'product_nutrition', type_='unique')
    op.drop_constraint('uq_product_nutrition_product_key', 'product_nutrition', type_='unique')
    op.create_unique_constraint("uq_product_nutrition_product_key", "product_nutrition", ["product_id", "key", "deleted_at"])
    
    op.drop_constraint('uq_product_sections', 'product_sections', type_='unique')
    op.drop_constraint('uq_product_sections_product_title', 'product_sections', type_='unique')
    op.create_unique_constraint("uq_product_sections_product_title", "product_sections", ["product_id", "title", "deleted_at"])
    
    op.drop_constraint('uq_product_stickers', 'product_stickers', type_='unique')
    op.drop_constraint('uq_product_stickers_product_name', 'product_stickers', type_='unique')
    op.create_unique_constraint("uq_product_stickers_product_name", "product_stickers", ["product_id", "name", "deleted_at"])
    

def downgrade():
    # Restore old constraint
    
    op.drop_constraint('uq_product_images_product_url_kind', 'product_images', type_='unique')
    op.create_unique_constraint("uq_product_images_product_url_kind", "product_images", ["product_id", "url", "kind"])
    op.create_unique_constraint("uq_product_images", "product_images", ["product_id", "position", "deleted_at"])
    
    op.drop_constraint('uq_product_labels_product_name', 'product_labels', type_='unique')
    op.create_unique_constraint("uq_product_labels_product_name", "product_labels", ["product_id", "name"])
    op.create_unique_constraint("uq_product_labels", "product_labels", ["product_id", "name", "deleted_at"])
    
    op.drop_constraint('uq_product_nutrition_product_key', 'product_nutrition', type_='unique')
    op.create_unique_constraint("uq_product_nutrition_product_key", "product_nutrition", ["product_id", "key"])
    op.create_unique_constraint("uq_product_nutrition", "product_nutrition", ["product_id", "key", "deleted_at"])
    
    op.drop_constraint('uq_product_sections_product_title', 'product_sections', type_='unique')
    op.create_unique_constraint("uq_product_sections_product_title", "product_sections", ["product_id", "title"])
    op.create_unique_constraint("uq_product_sections", "product_sections", ["product_id", "title", "deleted_at"])
    
    op.drop_constraint('uq_product_stickers_product_name', 'product_stickers', type_='unique')
    op.create_unique_constraint("uq_product_stickers_product_name", "product_stickers", ["product_id", "name", ])
    op.create_unique_constraint("uq_product_stickers", "product_stickers", ["product_id", "name", "deleted_at" ])
    