BEGIN;

-- MARKETING_OUT: if AYAM-only rule exists, convert back to global rule.
UPDATE fifo_stock_v2_overconsume_rules
SET
    flag_group_code = NULL,
    allow_overconsume = FALSE,
    priority = 20,
    reason = 'fifo_v2_exception_marketing_block',
    is_active = TRUE
WHERE lane = 'USABLE'
  AND function_code = 'MARKETING_OUT'
  AND flag_group_code = 'AYAM'
  AND reason = 'fifo_v2_exception_marketing_block_ayam_only';

-- MARKETING_OUT: if global row already exists, keep it active.
UPDATE fifo_stock_v2_overconsume_rules
SET
    allow_overconsume = FALSE,
    priority = 20,
    is_active = TRUE
WHERE lane = 'USABLE'
  AND function_code = 'MARKETING_OUT'
  AND flag_group_code IS NULL
  AND reason = 'fifo_v2_exception_marketing_block';

-- MARKETING_OUT: insert global rule if still missing.
INSERT INTO fifo_stock_v2_overconsume_rules(
    flag_group_code,
    function_code,
    lane,
    allow_overconsume,
    priority,
    reason,
    is_active
)
SELECT NULL, 'MARKETING_OUT', 'USABLE', FALSE, 20, 'fifo_v2_exception_marketing_block', TRUE
WHERE NOT EXISTS (
    SELECT 1
    FROM fifo_stock_v2_overconsume_rules
    WHERE lane = 'USABLE'
      AND function_code = 'MARKETING_OUT'
      AND flag_group_code IS NULL
      AND reason = 'fifo_v2_exception_marketing_block'
);

-- MARKETING_OUT: deactivate AYAM-only duplicates if any remain.
UPDATE fifo_stock_v2_overconsume_rules
SET
    is_active = FALSE
WHERE lane = 'USABLE'
  AND function_code = 'MARKETING_OUT'
  AND flag_group_code = 'AYAM'
  AND reason = 'fifo_v2_exception_marketing_block_ayam_only';

-- STOCK_TRANSFER_OUT: if AYAM-only rule exists, convert back to global rule.
UPDATE fifo_stock_v2_overconsume_rules
SET
    flag_group_code = NULL,
    allow_overconsume = FALSE,
    priority = 30,
    reason = 'fifo_v2_exception_transfer_block',
    is_active = TRUE
WHERE lane = 'USABLE'
  AND function_code = 'STOCK_TRANSFER_OUT'
  AND flag_group_code = 'AYAM'
  AND reason = 'fifo_v2_exception_transfer_block_ayam_only';

-- STOCK_TRANSFER_OUT: if global row already exists, keep it active.
UPDATE fifo_stock_v2_overconsume_rules
SET
    allow_overconsume = FALSE,
    priority = 30,
    is_active = TRUE
WHERE lane = 'USABLE'
  AND function_code = 'STOCK_TRANSFER_OUT'
  AND flag_group_code IS NULL
  AND reason = 'fifo_v2_exception_transfer_block';

-- STOCK_TRANSFER_OUT: insert global rule if still missing.
INSERT INTO fifo_stock_v2_overconsume_rules(
    flag_group_code,
    function_code,
    lane,
    allow_overconsume,
    priority,
    reason,
    is_active
)
SELECT NULL, 'STOCK_TRANSFER_OUT', 'USABLE', FALSE, 30, 'fifo_v2_exception_transfer_block', TRUE
WHERE NOT EXISTS (
    SELECT 1
    FROM fifo_stock_v2_overconsume_rules
    WHERE lane = 'USABLE'
      AND function_code = 'STOCK_TRANSFER_OUT'
      AND flag_group_code IS NULL
      AND reason = 'fifo_v2_exception_transfer_block'
);

-- STOCK_TRANSFER_OUT: deactivate AYAM-only duplicates if any remain.
UPDATE fifo_stock_v2_overconsume_rules
SET
    is_active = FALSE
WHERE lane = 'USABLE'
  AND function_code = 'STOCK_TRANSFER_OUT'
  AND flag_group_code = 'AYAM'
  AND reason = 'fifo_v2_exception_transfer_block_ayam_only';

-- CHICKIN_OUT: rollback AYAM-only hard-block added by up migration.
UPDATE fifo_stock_v2_overconsume_rules
SET
    is_active = FALSE
WHERE lane = 'USABLE'
  AND function_code = 'CHICKIN_OUT'
  AND flag_group_code = 'AYAM'
  AND reason = 'fifo_v2_exception_chickin_block_ayam_only';

COMMIT;
