SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

ALTER TABLE admin_user_withdrawal_applications
  ADD COLUMN salary_asset_type VARCHAR(64) NOT NULL DEFAULT '' AFTER user_id,
  ADD COLUMN withdraw_amount_minor BIGINT NOT NULL DEFAULT 0 AFTER withdraw_amount,
  ADD COLUMN freeze_command_id VARCHAR(128) NOT NULL DEFAULT '' AFTER withdraw_address,
  ADD COLUMN freeze_transaction_id VARCHAR(128) NOT NULL DEFAULT '' AFTER freeze_command_id,
  ADD COLUMN audit_command_id VARCHAR(128) NOT NULL DEFAULT '' AFTER freeze_transaction_id,
  ADD COLUMN audit_transaction_id VARCHAR(128) NOT NULL DEFAULT '' AFTER audit_command_id,
  ADD COLUMN audit_remark TEXT NULL AFTER approver_name;

SET @now_ms = CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED);

INSERT INTO admin_permissions (name, code, kind, description, created_at_ms, updated_at_ms) VALUES
  ('用户提现申请审核', 'finance-withdrawal:audit', 'button', '允许审核用户提现申请并触发工资冻结扣减或返还', @now_ms, @now_ms)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  kind = VALUES(kind),
  description = VALUES(description),
  updated_at_ms = @now_ms;

INSERT IGNORE INTO admin_role_permissions (role_id, permission_id)
SELECT admin_role.id, admin_permission.id
FROM admin_roles admin_role
JOIN admin_permissions admin_permission
WHERE admin_role.code = 'platform-admin'
  AND admin_permission.code = 'finance-withdrawal:audit';
