SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Explore tab 是 App 运行时配置，后台保存全量配置，gateway 只下发启用项。
SET @now_ms = CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED);

CREATE TABLE IF NOT EXISTS admin_app_explore_tabs (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '主键 ID',
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  tab VARCHAR(80) NOT NULL COMMENT 'Explore tab 展示文案',
  h5_url VARCHAR(2048) NOT NULL COMMENT 'Explore tab H5 链接',
  enabled BOOLEAN NOT NULL DEFAULT TRUE COMMENT '是否启用',
  sort_order INT NOT NULL DEFAULT 0 COMMENT '排序权重，数值越小越靠前',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  UNIQUE KEY uk_admin_app_explore_tabs_tab (app_code, tab),
  INDEX idx_admin_app_explore_tabs_app_sort (app_code, enabled, sort_order, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='后台 App Explore tab 配置表';

INSERT INTO admin_menus (parent_id, title, code, path, icon, permission_code, sort, visible, created_at_ms, updated_at_ms)
SELECT parent.id, 'Explore配置', 'app-config-explore', '/app-config/explore', 'explore', '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 = 'payment-recharge-products' AND sort < 69;

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