SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 开屏配置和 Banner 共用投放维度，但没有显示范围和房间小图；gateway 只读取当前有效窗口。
SET @now_ms = CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED);

CREATE TABLE IF NOT EXISTS admin_app_splash_screens (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '主键 ID',
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  cover_url VARCHAR(1024) NOT NULL COMMENT '开屏图片 URL',
  splash_type VARCHAR(16) NOT NULL COMMENT '开屏跳转类型',
  param VARCHAR(2048) NOT NULL DEFAULT '' COMMENT '跳转参数',
  status VARCHAR(24) NOT NULL DEFAULT 'active' COMMENT '业务状态',
  platform VARCHAR(24) NOT NULL COMMENT '平台',
  sort_order INT NOT NULL DEFAULT 0 COMMENT '排序权重，数值越小越靠前',
  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '区域 ID，0 表示全部区域',
  country_code VARCHAR(8) NOT NULL DEFAULT '' COMMENT '国家或地区编码，空表示全部国家',
  description VARCHAR(255) NOT NULL DEFAULT '' COMMENT '描述信息',
  starts_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '投放开始时间，UTC epoch ms',
  ends_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '投放结束时间，UTC epoch ms',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  INDEX idx_admin_app_splash_screens_app_sort (app_code, status, sort_order, id),
  INDEX idx_admin_app_splash_screens_scope (app_code, platform, region_id, country_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='后台 App 开屏配置表';

INSERT INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, '开屏配置', 'app-config-splash-screens', '/app-config/splash-screens', 'image', 'app-config:view', 68, TRUE, @now_ms, @now_ms
FROM admin_menus parent
WHERE parent.code = 'app-config'
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;

UPDATE admin_menus
SET sort = 69, updated_at_ms = @now_ms
WHERE code = 'app-config-explore' AND sort < 69;

UPDATE admin_menus
SET sort = 70, updated_at_ms = @now_ms
WHERE code = 'payment-recharge-products' AND sort < 70;

UPDATE admin_menus
SET sort = 71, updated_at_ms = @now_ms
WHERE code = 'app-config-versions' AND sort < 71;
