-- Move FIFO fields from laying_transfers to detail tables (sources & targets)
-- This enables proper FIFO integration for transfer laying with multiple sources and targets

-- ============================================================================
-- PART 1: Remove FIFO-related columns from laying_transfers table
-- ============================================================================

-- Drop foreign key constraints first
DO $$
BEGIN
    -- Drop source product warehouse FK
    IF EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'fk_laying_transfers_product_warehouse_id'
    ) THEN
        ALTER TABLE laying_transfers
            DROP CONSTRAINT fk_laying_transfers_product_warehouse_id;
    END IF;

    -- Drop destination product warehouse FK
    IF EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'fk_laying_transfers_dest_product_warehouse_id'
    ) THEN
        ALTER TABLE laying_transfers
            DROP CONSTRAINT fk_laying_transfers_dest_product_warehouse_id;
    END IF;
END $$;

-- Drop indexes
DROP INDEX IF EXISTS idx_laying_transfers_product_warehouse_id;
DROP INDEX IF EXISTS idx_laying_transfers_dest_product_warehouse_id;

-- Remove columns from laying_transfers
ALTER TABLE laying_transfers
    DROP COLUMN IF EXISTS product_warehouse_id,
    DROP COLUMN IF EXISTS dest_product_warehouse_id,
    DROP COLUMN IF EXISTS pending_usage_qty,
    DROP COLUMN IF EXISTS usage_qty,
    DROP COLUMN IF EXISTS total_qty,
    DROP COLUMN IF EXISTS total_used;

-- ============================================================================
-- PART 2: Add FIFO columns to laying_transfer_sources (USABLE role)
-- ============================================================================

ALTER TABLE laying_transfer_sources
    ADD COLUMN usage_qty NUMERIC(15, 3) DEFAULT 0 NOT NULL,
    ADD COLUMN pending_usage_qty NUMERIC(15, 3) DEFAULT 0 NOT NULL;

-- Add comments for documentation
COMMENT ON COLUMN laying_transfer_sources.usage_qty IS 'Quantity consumed from this source - for FIFO USABLE role';
COMMENT ON COLUMN laying_transfer_sources.pending_usage_qty IS 'Quantity pending to consume from this source - for FIFO USABLE role';

-- Drop old qty column as it's replaced by usage_qty
ALTER TABLE laying_transfer_sources
    DROP COLUMN IF EXISTS qty;

-- ============================================================================
-- PART 3: Add FIFO columns to laying_transfer_targets (STOCKABLE role)
-- ============================================================================

ALTER TABLE laying_transfer_targets
    ADD COLUMN total_qty NUMERIC(15, 3) DEFAULT 0 NOT NULL,
    ADD COLUMN total_used NUMERIC(15, 3) DEFAULT 0 NOT NULL;

-- Add comments for documentation
COMMENT ON COLUMN laying_transfer_targets.total_qty IS 'Total lot quantity introduced to this target warehouse - for FIFO STOCKABLE role';
COMMENT ON COLUMN laying_transfer_targets.total_used IS 'Quantity already consumed from this lot at target warehouse - for FIFO STOCKABLE role';

-- Drop old qty column as it's replaced by total_qty
ALTER TABLE laying_transfer_targets
    DROP COLUMN IF EXISTS qty;
