SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 弹窗配置是 App 启动/回前台读取的轻量运营配置；图片可为空，展示周期由客户端本地频控执行。
SET @now_ms = CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED);

CREATE TABLE IF NOT EXISTS admin_app_popups (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '主键 ID',
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  code VARCHAR(80) NOT NULL COMMENT '弹窗编码，用于客户端本地频控和埋点识别',
  name VARCHAR(80) NOT NULL COMMENT '弹窗名称',
  image_url VARCHAR(1024) NOT NULL DEFAULT '' COMMENT '弹窗图片 URL，可为空',
  jump_type VARCHAR(16) NOT NULL COMMENT '跳转类型：h5 或 app',
  jump_url VARCHAR(2048) NOT NULL DEFAULT '' COMMENT '跳转地址',
  display_period_days INT NOT NULL DEFAULT 0 COMMENT '展示周期天数，0 表示每次打开都展示',
  status VARCHAR(24) NOT NULL DEFAULT 'active' COMMENT '业务状态',
  sort_order INT NOT NULL DEFAULT 0 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',
  UNIQUE KEY uk_admin_app_popups_code (app_code, code),
  INDEX idx_admin_app_popups_app_sort (app_code, status, sort_order, id),
  INDEX idx_admin_app_popups_window (app_code, status, starts_at_ms, ends_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='后台 App 弹窗配置表';

-- 开发阶段如果已经执行过旧版 049，这里补齐 code 列，并用历史 ID 生成稳定默认值，避免多个空 code 破坏唯一索引。
SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'admin_app_popups' AND COLUMN_NAME = 'code') = 0,
  'ALTER TABLE admin_app_popups ADD COLUMN code VARCHAR(80) NOT NULL DEFAULT '''' COMMENT ''弹窗编码，用于客户端本地频控和埋点识别'' AFTER app_code',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

UPDATE admin_app_popups
SET code = CONCAT('popup-', id)
WHERE code = '';

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'admin_app_popups' AND INDEX_NAME = 'uk_admin_app_popups_code') = 0,
  'ALTER TABLE admin_app_popups ADD UNIQUE KEY uk_admin_app_popups_code (app_code, code)',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

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-popups', '/app-config/popups', 'image', 'app-config:view', 69, 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 = 70, updated_at_ms = @now_ms
WHERE code = 'app-config-explore' AND sort < 70;

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