-- Rollback: Revert FIFO fields back to laying_transfers from detail tables

-- ============================================================================
-- PART 1: Remove FIFO columns from detail tables
-- ============================================================================

-- Add back old qty column first
ALTER TABLE laying_transfer_sources
    ADD COLUMN IF NOT EXISTS qty NUMERIC(15, 3) NOT NULL DEFAULT 0;

ALTER TABLE laying_transfer_targets
    ADD COLUMN IF NOT EXISTS qty NUMERIC(15, 3) NOT NULL DEFAULT 0;

-- Now drop FIFO columns
ALTER TABLE laying_transfer_sources
    DROP COLUMN IF EXISTS usage_qty,
    DROP COLUMN IF EXISTS pending_usage_qty;

ALTER TABLE laying_transfer_targets
    DROP COLUMN IF EXISTS total_qty,
    DROP COLUMN IF EXISTS total_used;

-- ============================================================================
-- PART 2: Add back FIFO columns to laying_transfers table
-- ============================================================================

-- Add columns back for USABLE role (source warehouse)
ALTER TABLE laying_transfers
    ADD COLUMN product_warehouse_id BIGINT,
    ADD COLUMN pending_usage_qty NUMERIC(15, 3),
    ADD COLUMN usage_qty NUMERIC(15, 3);

-- Add columns back for STOCKABLE role (destination warehouse)
ALTER TABLE laying_transfers
    ADD COLUMN dest_product_warehouse_id BIGINT,
    ADD COLUMN total_qty NUMERIC(15, 3) DEFAULT 0 NOT NULL,
    ADD COLUMN total_used NUMERIC(15, 3) DEFAULT 0 NOT NULL;

-- ============================================================================
-- PART 3: Recreate foreign key constraints
-- ============================================================================

DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'product_warehouses') THEN
        -- Add source product warehouse FK
        ALTER TABLE laying_transfers
            ADD CONSTRAINT fk_laying_transfers_product_warehouse_id
            FOREIGN KEY (product_warehouse_id)
            REFERENCES product_warehouses(id)
            ON DELETE SET NULL;

        -- Add destination product warehouse FK
        ALTER TABLE laying_transfers
            ADD CONSTRAINT fk_laying_transfers_dest_product_warehouse_id
            FOREIGN KEY (dest_product_warehouse_id)
            REFERENCES product_warehouses(id)
            ON DELETE SET NULL;
    END IF;
END $$;

-- ============================================================================
-- PART 4: Recreate indexes for performance
-- ============================================================================

CREATE INDEX idx_laying_transfers_product_warehouse_id
    ON laying_transfers(product_warehouse_id);

CREATE INDEX idx_laying_transfers_dest_product_warehouse_id
    ON laying_transfers(dest_product_warehouse_id);

-- ============================================================================
-- PART 5: Recreate comments for documentation
-- ============================================================================

COMMENT ON COLUMN laying_transfers.product_warehouse_id IS 'Product warehouse at source (Growing flock) - for USABLE role';
COMMENT ON COLUMN laying_transfers.dest_product_warehouse_id IS 'Product warehouse at destination (Laying flock) - for STOCKABLE role';
COMMENT ON COLUMN laying_transfers.total_qty IS 'Total lot quantity introduced to destination warehouse - for STOCKABLE role';
COMMENT ON COLUMN laying_transfers.total_used IS 'Quantity already consumed from this lot at destination - for FIFO STOCKABLE role';
