SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

USE hyapp_user;

SET @now_ms = CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED);

-- apps 同时按 app_code 和 package_name/platform 唯一；包名若已属于别的 app，必须中止，避免 upsert 误改非 Yumi 行。
CREATE TEMPORARY TABLE yumi_app_package_conflict_guard (
  guard_id TINYINT NOT NULL PRIMARY KEY
) ENGINE=Memory;
INSERT INTO yumi_app_package_conflict_guard (guard_id) VALUES (1);
INSERT INTO yumi_app_package_conflict_guard (guard_id)
SELECT 1
FROM apps
WHERE package_name = 'com.org.yumi'
  AND platform = ''
  AND app_code <> 'yumi'
LIMIT 1;
DROP TEMPORARY TABLE yumi_app_package_conflict_guard;

-- App 注册表是 gateway 按 app_code 或包名解析租户的事实来源；Yumi 本地联调必须先注册为 active。
INSERT INTO apps (app_code, app_name, package_name, platform, status, created_at_ms, updated_at_ms)
VALUES ('yumi', 'Yumi', 'com.org.yumi', '', 'active', @now_ms, @now_ms)
ON DUPLICATE KEY UPDATE
  app_name = VALUES(app_name),
  package_name = VALUES(package_name),
  platform = VALUES(platform),
  status = VALUES(status),
  updated_at_ms = VALUES(updated_at_ms);

-- 快捷建号和三方注册都会读取 app 级风控配置；补齐默认值避免 app 支持后注册链路再失败。
INSERT INTO auth_risk_configs (app_code, max_accounts_per_device, updated_by_admin_id, created_at_ms, updated_at_ms)
VALUES ('yumi', 1, 0, @now_ms, @now_ms)
ON DUPLICATE KEY UPDATE
  app_code = app_code;

-- 注册资料会校验 countries；本地 Yumi 没有国家主数据时快捷建号会被 country is not supported 拦住。
INSERT INTO countries (
  app_code, country_name, country_code, iso_alpha3, iso_numeric, country_display_name,
  phone_country_code, flag, enabled, sort_order, created_by_user_id, updated_by_user_id, created_at_ms, updated_at_ms
)
SELECT
  'yumi', country_name, country_code, iso_alpha3, iso_numeric, country_display_name,
  phone_country_code, flag, enabled, sort_order, created_by_user_id, updated_by_user_id, @now_ms, @now_ms
FROM countries
WHERE app_code = 'lalu'
ON DUPLICATE KEY UPDATE
  country_name = VALUES(country_name),
  iso_alpha3 = VALUES(iso_alpha3),
  iso_numeric = VALUES(iso_numeric),
  country_display_name = VALUES(country_display_name),
  phone_country_code = VALUES(phone_country_code),
  flag = VALUES(flag),
  enabled = VALUES(enabled),
  sort_order = VALUES(sort_order),
  updated_by_user_id = VALUES(updated_by_user_id),
  updated_at_ms = VALUES(updated_at_ms);

-- 区域映射用于注册时把国家收敛到服务端 region_id；复制 Lalu 目录即可满足本地联调。
INSERT INTO regions (
  app_code, region_code, name, status, sort_order, created_by_user_id, updated_by_user_id, created_at_ms, updated_at_ms
)
SELECT
  'yumi', region_code, name, status, sort_order, created_by_user_id, updated_by_user_id, @now_ms, @now_ms
FROM regions
WHERE app_code = 'lalu'
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  status = VALUES(status),
  sort_order = VALUES(sort_order),
  updated_by_user_id = VALUES(updated_by_user_id),
  updated_at_ms = VALUES(updated_at_ms);

INSERT INTO region_countries (
  app_code, region_id, country_code, status, created_by_user_id, updated_by_user_id, created_at_ms, updated_at_ms
)
SELECT
  'yumi', target_region.region_id, source_mapping.country_code, source_mapping.status,
  source_mapping.created_by_user_id, source_mapping.updated_by_user_id, @now_ms, @now_ms
FROM region_countries source_mapping
JOIN regions source_region
  ON source_region.app_code = source_mapping.app_code
 AND source_region.region_id = source_mapping.region_id
JOIN regions target_region
  ON target_region.app_code = 'yumi'
 AND target_region.region_code = source_region.region_code
WHERE source_mapping.app_code = 'lalu'
ON DUPLICATE KEY UPDATE
  region_id = VALUES(region_id),
  status = VALUES(status),
  updated_by_user_id = VALUES(updated_by_user_id),
  updated_at_ms = VALUES(updated_at_ms);
