SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;
SET time_zone = '+00:00';

USE hyapp_wallet;

-- Agency 分成表通常远小于 wallet_entries，但仍属于不可重建的账务审计事实。先补兼容列，再发布
-- 新二进制；滚动期旧写入方没有公式基数，必须标记 unknown，不能伪装成 charge_amount=0。
SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'agency_point_share_entries'
     AND COLUMN_NAME = 'share_base_type') = 0,
  'ALTER TABLE agency_point_share_entries ADD COLUMN share_base_type VARCHAR(32) NOT NULL DEFAULT ''unknown'' COMMENT ''分成基数：unknown/charge_amount/host_income'' AFTER ratio_ppm, ALGORITHM=INPLACE, LOCK=NONE',
  '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 = 'agency_point_share_entries'
     AND COLUMN_NAME = 'share_base_amount') = 0,
  'ALTER TABLE agency_point_share_entries ADD COLUMN share_base_amount BIGINT NOT NULL DEFAULT 0 COMMENT ''本次分成计算基数；share_base_type=unknown 时数值不可解释'' AFTER share_base_type, ALGORITHM=INPLACE, LOCK=NONE',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 不在 DDL migration 中改 active wallet_policy_instances。旧二进制不理解 gift-type ratio 和
-- host_income，若先改运行快照会在滚动窗口把 lucky/super-lucky 错发成 100%/20%，self-owner 还会
-- 因重复余额事件回滚。Admin 115 把 Fami 实例置 draft；新 wallet 全部就绪后由显式 PublishInstance
-- 原子重建运行快照，保持 Admin 控制面状态与 wallet 实际策略一致。
