"""Standardize product references to inventory_product_id

Revision ID: 001_standardize_product_references
Revises: 
Create Date: 2025-01-15 12:00:00.000000

This migration standardizes product references across the suppliers service by:
1. Renaming ingredient_id columns to inventory_product_id
2. Removing redundant product_name columns where UUID references exist
3. Updating indexes to match new column names

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID


# revision identifiers
revision = '001_standardize_product_references'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    """Apply the changes to standardize product references"""
    
    # 1. Update supplier_price_lists table
    print("Updating supplier_price_lists table...")
    
    # Rename ingredient_id to inventory_product_id
    op.alter_column('supplier_price_lists', 'ingredient_id', 
                   new_column_name='inventory_product_id')
    
    # Drop the product_name column (redundant with UUID reference)
    op.drop_column('supplier_price_lists', 'product_name')
    
    # Update index name
    op.drop_index('ix_price_lists_ingredient')
    op.create_index('ix_price_lists_inventory_product', 'supplier_price_lists', 
                   ['inventory_product_id'])
    
    
    # 2. Update purchase_order_items table
    print("Updating purchase_order_items table...")
    
    # Rename ingredient_id to inventory_product_id
    op.alter_column('purchase_order_items', 'ingredient_id',
                   new_column_name='inventory_product_id')
    
    # Drop the product_name column (redundant with UUID reference)
    op.drop_column('purchase_order_items', 'product_name')
    
    # Update index name
    op.drop_index('ix_po_items_ingredient')
    op.create_index('ix_po_items_inventory_product', 'purchase_order_items',
                   ['inventory_product_id'])
    
    
    # 3. Update delivery_items table
    print("Updating delivery_items table...")
    
    # Rename ingredient_id to inventory_product_id
    op.alter_column('delivery_items', 'ingredient_id',
                   new_column_name='inventory_product_id')
    
    # Drop the product_name column (redundant with UUID reference)
    op.drop_column('delivery_items', 'product_name')
    
    # Update index name
    op.drop_index('ix_delivery_items_ingredient')
    op.create_index('ix_delivery_items_inventory_product', 'delivery_items',
                   ['inventory_product_id'])
    
    print("Migration completed successfully!")


def downgrade():
    """Revert the changes (for rollback purposes)"""
    
    print("Rolling back product reference standardization...")
    
    # 1. Revert delivery_items table
    print("Reverting delivery_items table...")
    
    # Revert index name
    op.drop_index('ix_delivery_items_inventory_product')
    op.create_index('ix_delivery_items_ingredient', 'delivery_items',
                   ['inventory_product_id'])  # Will rename back to ingredient_id below
    
    # Add back product_name column (will be empty initially)
    op.add_column('delivery_items', 
                 sa.Column('product_name', sa.String(255), nullable=False, 
                          server_default='Unknown Product'))
    
    # Rename inventory_product_id back to ingredient_id
    op.alter_column('delivery_items', 'inventory_product_id',
                   new_column_name='ingredient_id')
    
    # Update index to use ingredient_id
    op.drop_index('ix_delivery_items_ingredient')
    op.create_index('ix_delivery_items_ingredient', 'delivery_items',
                   ['ingredient_id'])
    
    
    # 2. Revert purchase_order_items table
    print("Reverting purchase_order_items table...")
    
    # Revert index name
    op.drop_index('ix_po_items_inventory_product')
    op.create_index('ix_po_items_ingredient', 'purchase_order_items',
                   ['inventory_product_id'])  # Will rename back to ingredient_id below
    
    # Add back product_name column (will be empty initially)
    op.add_column('purchase_order_items',
                 sa.Column('product_name', sa.String(255), nullable=False,
                          server_default='Unknown Product'))
    
    # Rename inventory_product_id back to ingredient_id
    op.alter_column('purchase_order_items', 'inventory_product_id',
                   new_column_name='ingredient_id')
    
    # Update index to use ingredient_id
    op.drop_index('ix_po_items_ingredient')
    op.create_index('ix_po_items_ingredient', 'purchase_order_items',
                   ['ingredient_id'])
    
    
    # 3. Revert supplier_price_lists table
    print("Reverting supplier_price_lists table...")
    
    # Revert index name
    op.drop_index('ix_price_lists_inventory_product')
    op.create_index('ix_price_lists_ingredient', 'supplier_price_lists',
                   ['inventory_product_id'])  # Will rename back to ingredient_id below
    
    # Add back product_name column (will be empty initially)
    op.add_column('supplier_price_lists',
                 sa.Column('product_name', sa.String(255), nullable=False,
                          server_default='Unknown Product'))
    
    # Rename inventory_product_id back to ingredient_id
    op.alter_column('supplier_price_lists', 'inventory_product_id',
                   new_column_name='ingredient_id')
    
    # Update index to use ingredient_id
    op.drop_index('ix_price_lists_ingredient')
    op.create_index('ix_price_lists_ingredient', 'supplier_price_lists',
                   ['ingredient_id'])
    
    print("Rollback completed successfully!")