-- Enable newly configured MiFaPay methods for LIKEI.
-- Requires the base channel rows from 20260430_mifapay_full_payment_country_channels.sql.

START TRANSACTION;

SET @factory_code := 'MIFA_PAY';
SET @sys_origin := 'LIKEI';
SET @region_relation_group := 'OPEN_PAY_COUNTRY';

DROP TEMPORARY TABLE IF EXISTS `tmp_mifapay_new_supported_channel`;
CREATE TEMPORARY TABLE `tmp_mifapay_new_supported_channel` (
  `country_code` VARCHAR(2) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `currency` VARCHAR(3) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `channel_code` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `factory_channel` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `suggest_score` INT NOT NULL,
  PRIMARY KEY (`country_code`, `currency`, `channel_code`, `factory_channel`)
) ENGINE=Memory;

INSERT INTO `tmp_mifapay_new_supported_channel`
  (`country_code`, `currency`, `channel_code`, `factory_channel`, `suggest_score`)
VALUES
  ('PH', 'PHP', 'Ewallet_Gcash', 'Gcash', 95),
  ('PH', 'PHP', 'Ewallet_PayMaya', 'PayMaya', 90),
  ('PH', 'PHP', 'QR_QRPH', 'QRPH', 100),
  ('BD', 'BDT', 'Ewallet_bKash', 'bKash', 95);

-- Enable the specific MiFaPay country/channel details.
UPDATE `sys_pay_country_channel_details` d
JOIN `sys_pay_country` pc ON pc.`id` = d.`pay_country_id`
JOIN `sys_country_code` cc ON cc.`id` = pc.`country_id`
JOIN `tmp_mifapay_new_supported_channel` s
  ON s.`country_code` = cc.`alpha_two`
  AND s.`currency` = pc.`currency`
  AND s.`channel_code` = d.`channel_code`
  AND s.`factory_channel` = d.`factory_channel`
SET d.`is_shelf` = 1,
    d.`factory_min_limit` = 0.01,
    d.`factory_max_limit` = 99999999.00,
    d.`factory_daily_limit` = 99999999.00,
    d.`suggest_score` = s.`suggest_score`,
    d.`update_time` = CURRENT_TIMESTAMP
WHERE d.`factory_code` = @factory_code;

-- Enable country/channel rows that now have at least one supported MiFaPay detail.
UPDATE `sys_pay_country_channel` cch
JOIN `sys_pay_country` pc ON pc.`id` = cch.`pay_country_id`
JOIN `sys_country_code` cc ON cc.`id` = pc.`country_id`
JOIN `tmp_mifapay_new_supported_channel` s
  ON s.`country_code` = cc.`alpha_two`
  AND s.`currency` = pc.`currency`
  AND s.`channel_code` = cch.`channel_code`
SET cch.`is_shelf` = 1,
    cch.`update_time` = CURRENT_TIMESTAMP
WHERE EXISTS (
  SELECT 1
  FROM `sys_pay_country_channel_details` d
  WHERE d.`pay_country_id` = cch.`pay_country_id`
    AND d.`channel_code` = cch.`channel_code`
    AND d.`factory_code` = @factory_code
    AND d.`factory_channel` = s.`factory_channel`
    AND d.`is_shelf` = 1
);

-- Expose the related pay countries and LIKEI V2 region relations.
UPDATE `sys_pay_country` pc
JOIN `sys_country_code` cc ON cc.`id` = pc.`country_id`
JOIN (SELECT DISTINCT `country_code`, `currency` FROM `tmp_mifapay_new_supported_channel`) s
  ON s.`country_code` = cc.`alpha_two`
  AND s.`currency` = pc.`currency`
SET pc.`is_shelf` = 1,
    pc.`update_time` = CURRENT_TIMESTAMP
WHERE EXISTS (
  SELECT 1
  FROM `sys_pay_country_channel_details` d
  WHERE d.`pay_country_id` = pc.`id`
    AND d.`factory_code` = @factory_code
    AND d.`is_shelf` = 1
);

UPDATE `sys_region_relation` rr
JOIN `sys_pay_country` pc ON pc.`id` = rr.`relation_id`
JOIN `sys_country_code` cc ON cc.`id` = pc.`country_id`
JOIN (SELECT DISTINCT `country_code`, `currency` FROM `tmp_mifapay_new_supported_channel`) s
  ON s.`country_code` = cc.`alpha_two`
  AND s.`currency` = pc.`currency`
SET rr.`is_showcase` = 1,
    rr.`update_time` = CURRENT_TIMESTAMP
WHERE rr.`sys_origin` = @sys_origin
  AND rr.`group_type` = @region_relation_group
  AND pc.`is_shelf` = 1;

COMMIT;

-- Verification queries. Expected: 4, 0, 2, 2.
SELECT 'enabled_new_mifapay_details' AS `metric`, COUNT(*) AS `value`
FROM `sys_pay_country_channel_details` d
JOIN `sys_pay_country` pc ON pc.`id` = d.`pay_country_id`
JOIN `sys_country_code` cc ON cc.`id` = pc.`country_id`
JOIN `tmp_mifapay_new_supported_channel` s
  ON s.`country_code` = cc.`alpha_two`
  AND s.`currency` = pc.`currency`
  AND s.`channel_code` = d.`channel_code`
  AND s.`factory_channel` = d.`factory_channel`
WHERE d.`factory_code` = @factory_code
  AND d.`is_shelf` = 1;

SELECT 'missing_new_mifapay_details' AS `metric`, COUNT(*) AS `value`
FROM `tmp_mifapay_new_supported_channel` s
WHERE NOT EXISTS (
  SELECT 1
  FROM `sys_pay_country_channel_details` d
  JOIN `sys_pay_country` pc ON pc.`id` = d.`pay_country_id`
  JOIN `sys_country_code` cc ON cc.`id` = pc.`country_id`
  WHERE cc.`alpha_two` = s.`country_code`
    AND pc.`currency` = s.`currency`
    AND d.`channel_code` = s.`channel_code`
    AND d.`factory_code` = @factory_code
    AND d.`factory_channel` = s.`factory_channel`
    AND d.`is_shelf` = 1
);

SELECT 'enabled_new_mifapay_countries' AS `metric`, COUNT(DISTINCT pc.`id`) AS `value`
FROM `sys_pay_country` pc
JOIN `sys_country_code` cc ON cc.`id` = pc.`country_id`
JOIN (SELECT DISTINCT `country_code`, `currency` FROM `tmp_mifapay_new_supported_channel`) s
  ON s.`country_code` = cc.`alpha_two`
  AND s.`currency` = pc.`currency`
WHERE pc.`is_shelf` = 1;

SELECT 'enabled_new_mifapay_region_relations' AS `metric`, COUNT(DISTINCT rr.`id`) AS `value`
FROM `sys_region_relation` rr
JOIN `sys_pay_country` pc ON pc.`id` = rr.`relation_id`
JOIN `sys_country_code` cc ON cc.`id` = pc.`country_id`
JOIN (SELECT DISTINCT `country_code`, `currency` FROM `tmp_mifapay_new_supported_channel`) s
  ON s.`country_code` = cc.`alpha_two`
  AND s.`currency` = pc.`currency`
WHERE rr.`sys_origin` = @sys_origin
  AND rr.`group_type` = @region_relation_group
  AND rr.`is_showcase` = 1;
