SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 首充奖励从“首笔金币区间”改为“Google 商品档位”：一个用户每个商品档位只能领取一次。

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'first_recharge_reward_tiers' AND COLUMN_NAME = 'usd_minor_amount') = 0,
  'ALTER TABLE first_recharge_reward_tiers ADD COLUMN usd_minor_amount BIGINT NOT NULL DEFAULT 0 COMMENT ''Google 商品美元美分档位'' AFTER max_coin_amount',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'first_recharge_reward_tiers' AND COLUMN_NAME = 'google_product_id') = 0,
  'ALTER TABLE first_recharge_reward_tiers ADD COLUMN google_product_id VARCHAR(128) NOT NULL DEFAULT '''' COMMENT ''Google Play 商品 ID'' AFTER usd_minor_amount',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'first_recharge_reward_tiers' AND COLUMN_NAME = 'discount_percent') = 0,
  'ALTER TABLE first_recharge_reward_tiers ADD COLUMN discount_percent INT NOT NULL DEFAULT 0 COMMENT ''展示优惠百分比，不参与结算'' AFTER google_product_id',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

UPDATE first_recharge_reward_tiers
SET google_product_id = CONCAT('legacy_', tier_id)
WHERE google_product_id = '';

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'first_recharge_reward_tiers' AND INDEX_NAME = 'uk_first_recharge_reward_google_product') = 0,
  'ALTER TABLE first_recharge_reward_tiers ADD UNIQUE KEY uk_first_recharge_reward_google_product (app_code, google_product_id)',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'first_recharge_reward_claims' AND COLUMN_NAME = 'tier_usd_minor_amount') = 0,
  'ALTER TABLE first_recharge_reward_claims ADD COLUMN tier_usd_minor_amount BIGINT NOT NULL DEFAULT 0 COMMENT ''命中档位美元美分'' AFTER resource_group_id',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'first_recharge_reward_claims' AND COLUMN_NAME = 'google_product_id') = 0,
  'ALTER TABLE first_recharge_reward_claims ADD COLUMN google_product_id VARCHAR(128) NOT NULL DEFAULT '''' COMMENT ''命中 Google Play 商品 ID'' AFTER tier_usd_minor_amount',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'first_recharge_reward_claims' AND COLUMN_NAME = 'recharge_usd_minor') = 0,
  'ALTER TABLE first_recharge_reward_claims ADD COLUMN recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT ''本次充值美元美分'' AFTER recharge_coin_amount',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'first_recharge_reward_claims' AND INDEX_NAME = 'uk_first_recharge_reward_user_once') > 0,
  'ALTER TABLE first_recharge_reward_claims DROP INDEX uk_first_recharge_reward_user_once',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'first_recharge_reward_claims' AND INDEX_NAME = 'uk_first_recharge_reward_user_tier') = 0,
  'ALTER TABLE first_recharge_reward_claims ADD UNIQUE KEY uk_first_recharge_reward_user_tier (app_code, user_id, tier_id)',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT
  COUNT(*) AS first_recharge_google_tier_count
FROM first_recharge_reward_tiers
WHERE google_product_id <> '';
