SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 使用 utf8mb4 连接字符集，保证中文注释、中文名称和 emoji 在导入时不被降级。

-- 后台导航是生产契约，不是本地演示数据。
-- 该迁移保证生产环境关闭 bootstrap seed 时，运营、活动和游戏菜单仍可重复初始化。
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
  ('金币流水查看', 'coin-ledger:view', 'menu', '', @now_ms, @now_ms),
  ('支付账单查看', 'payment-bill:view', 'menu', '', @now_ms, @now_ms),
  ('三方支付查看', 'payment-third-party:view', 'menu', '', @now_ms, @now_ms),
  ('三方支付更新', 'payment-third-party:update', 'button', '', @now_ms, @now_ms),
  ('内购配置查看', 'payment-product:view', 'menu', '', @now_ms, @now_ms),
  ('内购配置创建', 'payment-product:create', 'button', '', @now_ms, @now_ms),
  ('内购配置更新', 'payment-product:update', 'button', '', @now_ms, @now_ms),
  ('内购配置删除', 'payment-product:delete', 'button', '', @now_ms, @now_ms),
  ('游戏查看', 'game:view', 'menu', '', @now_ms, @now_ms),
  ('游戏创建', 'game:create', 'button', '', @now_ms, @now_ms),
  ('游戏更新', 'game:update', 'button', '', @now_ms, @now_ms),
  ('游戏状态', 'game:status', 'button', '', @now_ms, @now_ms),
  ('游戏删除', 'game:delete', 'button', '', @now_ms, @now_ms),
  ('每日任务查看', 'daily-task:view', 'menu', '', @now_ms, @now_ms),
  ('每日任务创建', 'daily-task:create', 'button', '', @now_ms, @now_ms),
  ('每日任务更新', 'daily-task:update', 'button', '', @now_ms, @now_ms),
  ('每日任务状态', 'daily-task:status', 'button', '', @now_ms, @now_ms),
  ('注册奖励查看', 'registration-reward:view', 'menu', '', @now_ms, @now_ms),
  ('注册奖励更新', 'registration-reward:update', 'button', '', @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) VALUES
  (NULL, 'APP配置', 'app-config', '', 'settings', '', 66, TRUE, @now_ms, @now_ms),
  (NULL, '运营管理', 'operations', '', 'operations', '', 68, TRUE, @now_ms, @now_ms),
  (NULL, '支付管理', 'payment', '', 'wallet', '', 69, TRUE, @now_ms, @now_ms),
  (NULL, '活动管理', 'activities', '', 'campaign', '', 70, TRUE, @now_ms, @now_ms),
  (NULL, '游戏管理', 'games', '', 'sports_esports', '', 71, TRUE, @now_ms, @now_ms)
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 INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '内购配置', 'payment-recharge-products', '/payment/recharge-products', 'wallet', 'payment-product:view', 70, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'payment'
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 INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '金币流水', 'operation-coin-ledger', '/operations/coin-ledger', 'receipt', 'coin-ledger:view', 68, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'operations'
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 INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '账单列表', 'payment-bill-list', '/payment/bills', 'receipt', 'payment-bill:view', 68, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'payment'
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 INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '三方支付', 'payment-third-party', '/payment/third-party', 'wallet', 'payment-third-party:view', 69, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'payment'
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 INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '每日任务', 'daily-task-list', '/activities/daily-tasks', 'task', 'daily-task:view', 69, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'activities'
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 INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '注册奖励', 'registration-reward', '/activities/registration-reward', 'gift', 'registration-reward:view', 70, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'activities'
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 INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '游戏列表', 'game-list', '/games', 'sports_esports', 'game:view', 70, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'games'
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 INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '自研游戏', 'self-games', '/games/self-games', 'settings', 'game:view', 80, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'games'
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 INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '全站机器人', 'game-robots', '/games/robots', 'team', 'game:view', 90, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'games'
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 INTO admin_roles (name, code, description, created_at_ms, updated_at_ms) VALUES
  ('平台管理员', 'platform-admin', '拥有管理后台全部权限', @now_ms, @now_ms)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  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';

INSERT IGNORE INTO admin_user_roles (user_id, role_id)
SELECT admin_user.id, admin_role.id
FROM admin_users admin_user
JOIN admin_roles admin_role
WHERE admin_user.username IN ('admin', 'hyappadmin') AND admin_role.code = 'platform-admin';
