SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 等级调整会跨 activity/wallet 执行奖励副作用，用户导出会读取筛选快照和生成文件；
-- 两者都必须与普通资料更新解耦，便于运营角色按职责最小授权。
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
  ('App 用户等级调整', 'app-user:level', 'button', '允许临时调整 App 用户富豪或魅力等级', @now_ms, @now_ms),
  ('App 用户导出', 'app-user:export', 'button', '允许按当前筛选快照异步导出 App 用户 CSV', @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 IN ('platform-admin', 'ops-admin')
  AND admin_permission.code IN ('app-user:level', 'app-user:export');
