-- ===============================================================
--  ADD FIFO FIELDS TO STOCK_TRANSFER_DETAILS
--  Enable transfer module to work with FIFO stock system
--
--  Notes:
--  - Field 'quantity' will be removed (replaced by usage_qty + pending_qty)
--  - Fields 'before_quantity' & 'after_quantity' will be removed (unused legacy)
--  - New FIFO fields track actual allocation instead of requested quantity
-- ===============================================================

-- Add FIFO tracking fields
ALTER TABLE stock_transfer_details
ADD COLUMN IF NOT EXISTS source_product_warehouse_id BIGINT,
ADD COLUMN IF NOT EXISTS dest_product_warehouse_id BIGINT,
ADD COLUMN IF NOT EXISTS usage_qty NUMERIC(15, 3) DEFAULT 0,
ADD COLUMN IF NOT EXISTS pending_qty NUMERIC(15, 3) DEFAULT 0,
ADD COLUMN IF NOT EXISTS total_qty NUMERIC(15, 3) DEFAULT 0,
ADD COLUMN IF NOT EXISTS total_used NUMERIC(15, 3) DEFAULT 0;

-- Remove obsolete columns (quantity replaced by FIFO fields, legacy fields never used)
ALTER TABLE stock_transfer_details
DROP COLUMN IF EXISTS quantity,
DROP COLUMN IF EXISTS before_quantity,
DROP COLUMN IF EXISTS after_quantity;

-- Add foreign keys for product warehouse references
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'product_warehouses') THEN
        -- Source warehouse foreign key
        IF NOT EXISTS (
            SELECT 1 FROM pg_constraint
            WHERE conname = 'fk_stock_transfer_details_source_pw'
        ) THEN
            EXECUTE
                'ALTER TABLE stock_transfer_details
                 ADD CONSTRAINT fk_stock_transfer_details_source_pw
                 FOREIGN KEY (source_product_warehouse_id)
                 REFERENCES product_warehouses(id)
                 ON DELETE SET NULL ON UPDATE CASCADE';
        END IF;

        -- Destination warehouse foreign key
        IF NOT EXISTS (
            SELECT 1 FROM pg_constraint
            WHERE conname = 'fk_stock_transfer_details_dest_pw'
        ) THEN
            EXECUTE
                'ALTER TABLE stock_transfer_details
                 ADD CONSTRAINT fk_stock_transfer_details_dest_pw
                 FOREIGN KEY (dest_product_warehouse_id)
                 REFERENCES product_warehouses(id)
                 ON DELETE SET NULL ON UPDATE CASCADE';
        END IF;
    END IF;
END $$;

-- Add indexes for FIFO operations
CREATE INDEX IF NOT EXISTS idx_stock_transfer_details_source_pw
ON stock_transfer_details (source_product_warehouse_id);

CREATE INDEX IF NOT EXISTS idx_stock_transfer_details_dest_pw
ON stock_transfer_details (dest_product_warehouse_id);

-- Add comments for documentation
COMMENT ON COLUMN stock_transfer_details.source_product_warehouse_id IS
'Source product warehouse ID - referensi warehouse asal (FIFO usable)';

COMMENT ON COLUMN stock_transfer_details.dest_product_warehouse_id IS
'Destination product warehouse ID - referensi warehouse tujuan (FIFO stockable)';

COMMENT ON COLUMN stock_transfer_details.usage_qty IS
'Actual quantity successfully taken from source warehouse (FIFO usable tracking) - replaces quantity field';

COMMENT ON COLUMN stock_transfer_details.pending_qty IS
'Quantity waiting for stock availability (FIFO usable tracking)';

COMMENT ON COLUMN stock_transfer_details.total_qty IS
'Total lot quantity available at destination warehouse (FIFO stockable tracking)';

COMMENT ON COLUMN stock_transfer_details.total_used IS
'Quantity already consumed from this lot at destination warehouse (FIFO stockable tracking)';

