-- Revert inventory_movements table changes
-- Add back product_id column
ALTER TABLE inventory_movements 
ADD COLUMN product_id UUID;

-- Copy item_id data back to product_id where item_type is 'PRODUCT'
UPDATE inventory_movements 
SET product_id = item_id 
WHERE item_type = 'PRODUCT';

-- Drop the new columns
ALTER TABLE inventory_movements 
DROP COLUMN item_id,
DROP COLUMN item_type;

-- Revert quantity columns to integer
ALTER TABLE inventory_movements 
ALTER COLUMN quantity TYPE INTEGER USING quantity::integer,
ALTER COLUMN previous_quantity TYPE INTEGER USING previous_quantity::integer,
ALTER COLUMN new_quantity TYPE INTEGER USING new_quantity::integer;

-- Revert cost columns to original precision
ALTER TABLE inventory_movements 
ALTER COLUMN unit_cost TYPE DECIMAL(10,2),
ALTER COLUMN total_cost TYPE DECIMAL(10,2);

-- Drop the new indexes
DROP INDEX IF EXISTS idx_inventory_movements_item_id;
DROP INDEX IF EXISTS idx_inventory_movements_item_type;
DROP INDEX IF EXISTS idx_inventory_movements_item_id_type; 