SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS admin_user_withdrawal_applications (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  app_code VARCHAR(32) NOT NULL,
  user_id VARCHAR(64) NOT NULL,
  withdraw_amount DECIMAL(18,2) NOT NULL DEFAULT 0.00,
  withdraw_method VARCHAR(64) NOT NULL DEFAULT '',
  withdraw_address VARCHAR(255) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL DEFAULT 'pending',
  approver_user_id BIGINT UNSIGNED NULL,
  approver_name VARCHAR(64) NOT NULL DEFAULT '',
  approved_at_ms BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (id),
  KEY idx_admin_withdrawal_app_status_time (app_code, status, created_at_ms),
  KEY idx_admin_withdrawal_user (user_id),
  KEY idx_admin_withdrawal_method (withdraw_method),
  KEY idx_admin_withdrawal_approver (approver_user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

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:view', 'menu', '允许查看财务系统内的用户提现申请列表', @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:view';
