-- 运行快照只保存后台发布的不可变政策；逐列检查使迁移可安全重放。
SET @ddl = IF((SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'host_agency_salary_policies' AND COLUMN_NAME = 'policy_type') = 0,
  'ALTER TABLE host_agency_salary_policies ADD COLUMN policy_type VARCHAR(32) NOT NULL DEFAULT ''SALARY_DIAMOND'' COMMENT ''政策类型：SALARY_DIAMOND/POINT_DIAMOND'' AFTER name, 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 = 'host_agency_salary_policies' AND COLUMN_NAME = 'point_diamonds_per_usd') = 0,
  'ALTER TABLE host_agency_salary_policies ADD COLUMN point_diamonds_per_usd BIGINT NOT NULL DEFAULT 0 COMMENT ''POINT_DIAMOND 每美元对应钻石积分'' AFTER gift_coin_to_diamond_ratio, 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 = 'host_agency_salary_policies' AND COLUMN_NAME = 'coins_per_usd') = 0,
  'ALTER TABLE host_agency_salary_policies ADD COLUMN coins_per_usd BIGINT NOT NULL DEFAULT 0 COMMENT ''POINT_DIAMOND 每美元对应金币'' AFTER point_diamonds_per_usd, 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 = 'host_agency_salary_policies' AND COLUMN_NAME = 'minimum_withdraw_usd_minor') = 0,
  'ALTER TABLE host_agency_salary_policies ADD COLUMN minimum_withdraw_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT ''POINT_DIAMOND 最低提现美元美分'' AFTER coins_per_usd, 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 = 'host_agency_salary_policies' AND COLUMN_NAME = 'withdraw_fee_bps') = 0,
  'ALTER TABLE host_agency_salary_policies ADD COLUMN withdraw_fee_bps INT NOT NULL DEFAULT 0 COMMENT ''POINT_DIAMOND 提现费率基点'' AFTER minimum_withdraw_usd_minor, 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 = 'host_agency_salary_policies' AND COLUMN_NAME = 'agency_point_share_bps') = 0,
  'ALTER TABLE host_agency_salary_policies ADD COLUMN agency_point_share_bps INT NOT NULL DEFAULT 0 COMMENT ''POINT_DIAMOND 代理分成占主播基础积分的基点'' AFTER withdraw_fee_bps, ALGORITHM=INPLACE, LOCK=NONE', 'SELECT 1');
PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 新表从空表开始，CREATE TABLE IF NOT EXISTS 可安全重放且不会扫描 wallet_transactions/wallet_entries 大流水。
-- Host/Agency 时间索引直接服务用户与日期范围聚合，不依赖 metadata_json 的 JSON_EXTRACT。
CREATE TABLE IF NOT EXISTS point_diamond_gift_income_entries (
  entry_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '投影流水 ID',
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，必须显式写入以保证多租户隔离',
  transaction_id VARCHAR(96) NOT NULL COMMENT '送礼钱包交易 ID',
  host_user_id BIGINT NOT NULL COMMENT '收礼主播用户 ID',
  agency_user_id BIGINT NOT NULL DEFAULT 0 COMMENT '本次代理分成收款用户 ID，0 表示无分成',
  sender_user_id BIGINT NOT NULL DEFAULT 0 COMMENT '送礼用户 ID',
  host_base_amount BIGINT NOT NULL COMMENT '本次 Host 基础钻石积分，禁止包含 Agency 分成',
  agency_amount BIGINT NOT NULL DEFAULT 0 COMMENT '本次 Agency 钻石积分分成',
  created_at_ms BIGINT NOT NULL COMMENT '送礼成功时间，UTC epoch ms',
  UNIQUE KEY uk_point_diamond_gift_income_tx (app_code, transaction_id),
  KEY idx_point_diamond_gift_host_time (app_code, host_user_id, created_at_ms, entry_id),
  KEY idx_point_diamond_gift_agency_time (app_code, agency_user_id, created_at_ms, entry_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='POINT_DIAMOND 送礼收益角色拆分投影';

DROP TABLE IF EXISTS host_period_diamond_policy_behaviors;

-- 次数和 reservation 是小型聚合表；只补一列并改复合键，不回填或扫描 wallet_entries 大流水。
SET @ddl = IF((SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'point_withdrawal_policy_counters' AND COLUMN_NAME = 'source_asset_type') = 0,
  'ALTER TABLE point_withdrawal_policy_counters ADD COLUMN source_asset_type VARCHAR(32) NOT NULL DEFAULT ''POINT'' COMMENT ''提现来源资产'' AFTER app_code, ALGORITHM=INPLACE, LOCK=NONE', 'SELECT 1');
PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @counter_pk = (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY ORDINAL_POSITION SEPARATOR ',') FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'point_withdrawal_policy_counters' AND CONSTRAINT_NAME = 'PRIMARY');
SET @ddl = IF(@counter_pk <> 'app_code,source_asset_type,user_id,channel,period,period_key',
  'ALTER TABLE point_withdrawal_policy_counters DROP PRIMARY KEY, ADD PRIMARY KEY (app_code, source_asset_type, user_id, channel, period, period_key), 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 = 'point_withdrawal_limit_reservations' AND COLUMN_NAME = 'source_asset_type') = 0,
  'ALTER TABLE point_withdrawal_limit_reservations ADD COLUMN source_asset_type VARCHAR(32) NOT NULL DEFAULT ''POINT'' COMMENT ''提现来源资产'' AFTER app_code, ALGORITHM=INPLACE, LOCK=NONE', 'SELECT 1');
PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @reservation_pk = (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY ORDINAL_POSITION SEPARATOR ',') FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'point_withdrawal_limit_reservations' AND CONSTRAINT_NAME = 'PRIMARY');
SET @ddl = IF(@reservation_pk <> 'app_code,source_asset_type,withdrawal_ref',
  'ALTER TABLE point_withdrawal_limit_reservations DROP PRIMARY KEY, ADD PRIMARY KEY (app_code, source_asset_type, withdrawal_ref), ALGORITHM=INPLACE, LOCK=NONE', 'SELECT 1');
PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @reservation_uk = (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX SEPARATOR ',') FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'point_withdrawal_limit_reservations' AND INDEX_NAME = 'uk_point_withdrawal_limit_reservation_command');
SET @ddl = CASE
  WHEN @reservation_uk IS NULL THEN
    'ALTER TABLE point_withdrawal_limit_reservations ADD UNIQUE KEY uk_point_withdrawal_limit_reservation_command (app_code, source_asset_type, freeze_command_id), ALGORITHM=INPLACE, LOCK=NONE'
  WHEN @reservation_uk <> 'app_code,source_asset_type,freeze_command_id' THEN
    'ALTER TABLE point_withdrawal_limit_reservations DROP INDEX uk_point_withdrawal_limit_reservation_command, ADD UNIQUE KEY uk_point_withdrawal_limit_reservation_command (app_code, source_asset_type, freeze_command_id), ALGORITHM=INPLACE, LOCK=NONE'
  ELSE 'SELECT 1'
END;
PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt;
