SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 币商提现白名单是按当前后台 App 范围编辑的公共能力；不在表中复制 Who/Sofun 等固定开关，
-- 新增 App 时复用 X-App-Code 作用域即可，避免每增加一套公会页面就增加一列。
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
  ('币商提现白名单查看', 'point-withdrawal-config:view', 'menu', '查看当前 App 的 POINT 提现币商、服务国家和兑换比例', @now_ms, @now_ms),
  ('币商提现白名单编辑', 'point-withdrawal-config:update', 'button', '新增、编辑、禁用或删除当前 App 的 POINT 提现币商配置', @now_ms, @now_ms)
ON DUPLICATE KEY UPDATE
  name = VALUES(name), kind = VALUES(kind), description = VALUES(description), updated_at_ms = @now_ms;

INSERT INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '币商提现白名单', 'host-org-point-withdrawal-config', '/host/point-withdrawal-config', 'wallet', 'point-withdrawal-config:view', 89, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'host-org'
ON DUPLICATE KEY UPDATE
  parent_id = VALUES(parent_id), title = VALUES(title), path = VALUES(path), icon = VALUES(icon),
  permission_code = VALUES(permission_code), sort = VALUES(sort), visible = VALUES(visible), updated_at_ms = @now_ms;

INSERT IGNORE INTO admin_role_permissions (role_id, permission_id)
SELECT role.id, permission.id
FROM admin_roles role
JOIN admin_permissions permission
WHERE role.code IN ('platform-admin', 'ops-admin')
  AND permission.code IN ('point-withdrawal-config:view', 'point-withdrawal-config:update');

INSERT IGNORE INTO admin_role_permissions (role_id, permission_id)
SELECT role.id, permission.id
FROM admin_roles role
JOIN admin_permissions permission
WHERE role.code IN ('auditor', 'readonly')
  AND permission.code = 'point-withdrawal-config:view';
