BEGIN;

-- Disable CHICKIN as FIFO USABLE so chick-in acts as business tagging/conversion,
-- not physical stock consumption.
UPDATE fifo_stock_v2_route_rules
SET is_active = FALSE,
    updated_at = NOW()
WHERE flag_group_code = 'AYAM'
  AND lane = 'USABLE'
  AND function_code = 'CHICKIN_OUT'
  AND source_table = 'project_chickins'
  AND is_active = TRUE;

-- Release existing active allocations created by PROJECT_CHICKIN
-- and return warehouse qty back.
WITH released AS (
    UPDATE stock_allocations
    SET status = 'RELEASED',
        released_at = COALESCE(released_at, NOW()),
        updated_at = NOW(),
        note = CASE
            WHEN COALESCE(note, '') = '' THEN 'fifo_v2_chickin_conversion_release'
            ELSE note || '; fifo_v2_chickin_conversion_release'
        END
    WHERE usable_type = 'PROJECT_CHICKIN'
      AND status = 'ACTIVE'
    RETURNING product_warehouse_id, qty
),
pw_delta AS (
    SELECT product_warehouse_id, COALESCE(SUM(qty), 0) AS qty_delta
    FROM released
    GROUP BY product_warehouse_id
)
UPDATE product_warehouses pw
SET qty = COALESCE(pw.qty, 0) + d.qty_delta
FROM pw_delta d
WHERE pw.id = d.product_warehouse_id;

-- Resync stockable total_used columns from remaining ACTIVE allocations.

-- purchase_items (PURCHASE_ITEMS)
UPDATE purchase_items pi
SET total_used = COALESCE(a.used, 0)
FROM (
    SELECT stockable_id, SUM(qty) AS used
    FROM stock_allocations
    WHERE status = 'ACTIVE'
      AND stockable_type = 'PURCHASE_ITEMS'
    GROUP BY stockable_id
) a
WHERE pi.id = a.stockable_id;

UPDATE purchase_items pi
SET total_used = 0
WHERE NOT EXISTS (
    SELECT 1
    FROM stock_allocations sa
    WHERE sa.status = 'ACTIVE'
      AND sa.stockable_type = 'PURCHASE_ITEMS'
      AND sa.stockable_id = pi.id
);

-- stock_transfer_details (STOCK_TRANSFER_IN)
UPDATE stock_transfer_details std
SET total_used = COALESCE(a.used, 0)
FROM (
    SELECT stockable_id, SUM(qty) AS used
    FROM stock_allocations
    WHERE status = 'ACTIVE'
      AND stockable_type = 'STOCK_TRANSFER_IN'
    GROUP BY stockable_id
) a
WHERE std.id = a.stockable_id;

UPDATE stock_transfer_details std
SET total_used = 0
WHERE NOT EXISTS (
    SELECT 1
    FROM stock_allocations sa
    WHERE sa.status = 'ACTIVE'
      AND sa.stockable_type = 'STOCK_TRANSFER_IN'
      AND sa.stockable_id = std.id
);

-- adjustment_stocks (ADJUSTMENT_IN)
UPDATE adjustment_stocks ast
SET total_used = COALESCE(a.used, 0)
FROM (
    SELECT stockable_id, SUM(qty) AS used
    FROM stock_allocations
    WHERE status = 'ACTIVE'
      AND stockable_type = 'ADJUSTMENT_IN'
    GROUP BY stockable_id
) a
WHERE ast.id = a.stockable_id;

UPDATE adjustment_stocks ast
SET total_used = 0
WHERE NOT EXISTS (
    SELECT 1
    FROM stock_allocations sa
    WHERE sa.status = 'ACTIVE'
      AND sa.stockable_type = 'ADJUSTMENT_IN'
      AND sa.stockable_id = ast.id
);

-- laying_transfer_targets (TRANSFERTOLAYING_IN)
UPDATE laying_transfer_targets ltt
SET total_used = COALESCE(a.used, 0)
FROM (
    SELECT stockable_id, SUM(qty) AS used
    FROM stock_allocations
    WHERE status = 'ACTIVE'
      AND stockable_type = 'TRANSFERTOLAYING_IN'
    GROUP BY stockable_id
) a
WHERE ltt.id = a.stockable_id;

UPDATE laying_transfer_targets ltt
SET total_used = 0
WHERE NOT EXISTS (
    SELECT 1
    FROM stock_allocations sa
    WHERE sa.status = 'ACTIVE'
      AND sa.stockable_type = 'TRANSFERTOLAYING_IN'
      AND sa.stockable_id = ltt.id
);

-- recording_eggs (RECORDING_EGG)
UPDATE recording_eggs re
SET total_used = COALESCE(a.used, 0)
FROM (
    SELECT stockable_id, SUM(qty) AS used
    FROM stock_allocations
    WHERE status = 'ACTIVE'
      AND stockable_type = 'RECORDING_EGG'
    GROUP BY stockable_id
) a
WHERE re.id = a.stockable_id;

UPDATE recording_eggs re
SET total_used = 0
WHERE NOT EXISTS (
    SELECT 1
    FROM stock_allocations sa
    WHERE sa.status = 'ACTIVE'
      AND sa.stockable_type = 'RECORDING_EGG'
      AND sa.stockable_id = re.id
);

COMMIT;
