SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

USE hyapp_lucky_gift;

-- 性能边界：两张表都只增加常量默认列，强制 ALGORITHM=INSTANT，不扫描或回写任何历史行。
-- lucky_gift_rule_versions 是发布次数级的小配置表；lucky_pools 是奖池数量级的小账本表。
-- 若目标 MySQL 不支持即时 DDL 则直接失败，不能退化为 COPY；执行前仍需确认无长事务占用元数据锁。

-- fixed_v2 高水位返奖加权三元组：阈值 0 表示关闭，历史版本三列均为默认 0，语义不变。
SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_rule_versions'
     AND COLUMN_NAME = 'v2_high_water_boost_threshold_coins') = 0,
  'ALTER TABLE lucky_gift_rule_versions ADD COLUMN v2_high_water_boost_threshold_coins BIGINT NOT NULL DEFAULT 0 COMMENT ''fixed_v2 高水位返奖加权触发阈值金币；0表示关闭'' AFTER high_water_nonzero_factor_ppm, ALGORITHM=INSTANT',
  '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 = 'lucky_gift_rule_versions'
     AND COLUMN_NAME = 'v2_high_water_boost_factor_ppm') = 0,
  'ALTER TABLE lucky_gift_rule_versions ADD COLUMN v2_high_water_boost_factor_ppm BIGINT NOT NULL DEFAULT 0 COMMENT ''fixed_v2 高水位返奖加权系数，ppm；1500000表示RTP放大到150%'' AFTER v2_high_water_boost_threshold_coins, ALGORITHM=INSTANT',
  '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 = 'lucky_gift_rule_versions'
     AND COLUMN_NAME = 'v2_high_water_boost_recover_coins') = 0,
  'ALTER TABLE lucky_gift_rule_versions ADD COLUMN v2_high_water_boost_recover_coins BIGINT NOT NULL DEFAULT 0 COMMENT ''fixed_v2 高水位加权恢复水位金币；水位低于该值退出加权'' AFTER v2_high_water_boost_factor_ppm, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 滞回标志属于奖池账本行：加权是否生效取决于历史轨迹（200w 触发后要跌破 100w 才恢复），
-- 不能仅凭当前余额推导。默认 0 表示未处于加权区间，下一次开奖会按最新水位重新评估。
SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_pools'
     AND COLUMN_NAME = 'v2_boost_active') = 0,
  'ALTER TABLE lucky_pools ADD COLUMN v2_boost_active TINYINT(1) NOT NULL DEFAULT 0 COMMENT ''fixed_v2 高水位返奖加权滞回标志；1表示当前处于加权区间'' AFTER manual_debit_total, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt;
