SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 外部来源必须把申请时的费率、手续费和实际打款额作为不可变事实提交。
-- 四列均允许 NULL 兼容历史申请；迁移只做 INSTANT 元数据变更，不建索引、不扫描或回填提现大表。
SET @service_charge_ratio_exists = (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'admin_user_withdrawal_applications'
    AND COLUMN_NAME = 'service_charge_ratio'
);
SET @service_charge_ratio_ddl = IF(
  @service_charge_ratio_exists = 0,
  'ALTER TABLE admin_user_withdrawal_applications ADD COLUMN service_charge_ratio DECIMAL(9,4) NULL COMMENT ''来源申请时手续费率快照，单位为百分比点'' AFTER balance_before, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @service_charge_ratio_ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @service_charge_exists = (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'admin_user_withdrawal_applications'
    AND COLUMN_NAME = 'service_charge'
);
SET @service_charge_ddl = IF(
  @service_charge_exists = 0,
  'ALTER TABLE admin_user_withdrawal_applications ADD COLUMN service_charge DECIMAL(18,2) NULL COMMENT ''来源申请时手续费金额快照，单位为 USD'' AFTER service_charge_ratio, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @service_charge_ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @actual_amount_exists = (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'admin_user_withdrawal_applications'
    AND COLUMN_NAME = 'actual_amount'
);
SET @actual_amount_ddl = IF(
  @actual_amount_exists = 0,
  'ALTER TABLE admin_user_withdrawal_applications ADD COLUMN actual_amount DECIMAL(18,2) NULL COMMENT ''来源申请时实际打款金额快照，单位为 USD'' AFTER service_charge, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @actual_amount_ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @actual_amount_minor_exists = (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'admin_user_withdrawal_applications'
    AND COLUMN_NAME = 'actual_amount_minor'
);
SET @actual_amount_minor_ddl = IF(
  @actual_amount_minor_exists = 0,
  'ALTER TABLE admin_user_withdrawal_applications ADD COLUMN actual_amount_minor BIGINT NULL COMMENT ''来源申请时实际打款金额最小单位快照，USD 为美分'' AFTER actual_amount, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @actual_amount_minor_ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 外部来源财务回调不能与 Admin 本地终态共用一个事务：回调 2xx 后若本地提交失败，
-- nullable claim 字段保存第一次决策边界；审核人、备注、凭证、command 和时间复用现有财务审计列。
SET @external_finance_claim_decision_exists = (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'admin_user_withdrawal_applications'
    AND COLUMN_NAME = 'external_finance_claim_decision'
);
SET @external_finance_claim_decision_ddl = IF(
  @external_finance_claim_decision_exists = 0,
  'ALTER TABLE admin_user_withdrawal_applications ADD COLUMN external_finance_claim_decision VARCHAR(16) NULL COMMENT ''外部来源财务首次决策 claim'' AFTER actual_amount_minor, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @external_finance_claim_decision_ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @external_finance_claim_status_exists = (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'admin_user_withdrawal_applications'
    AND COLUMN_NAME = 'external_finance_claim_status'
);
SET @external_finance_claim_status_ddl = IF(
  @external_finance_claim_status_exists = 0,
  'ALTER TABLE admin_user_withdrawal_applications ADD COLUMN external_finance_claim_status VARCHAR(16) NULL COMMENT ''外部来源财务 claim 状态：claimed/finalized'' AFTER external_finance_claim_decision, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @external_finance_claim_status_ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
