SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE DATABASE IF NOT EXISTS hyapp_user DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE hyapp_user;

CREATE TABLE IF NOT EXISTS apps (
  app_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  app_code VARCHAR(32) NOT NULL,
  app_name VARCHAR(128) NOT NULL,
  package_name VARCHAR(191) NOT NULL,
  platform VARCHAR(32) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_apps_code (app_code),
  UNIQUE KEY uk_apps_package_platform (package_name, platform),
  KEY idx_apps_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO apps (app_code, app_name, package_name, platform, status, created_at_ms, updated_at_ms)
VALUES ('lalu', 'Lalu', 'com.org.laluparty', 'android', 'active', 0, 0)
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);

CREATE TABLE IF NOT EXISTS users (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL PRIMARY KEY,
  default_display_user_id VARCHAR(32) NOT NULL,
  current_display_user_id VARCHAR(32) NOT NULL,
  current_display_user_id_kind VARCHAR(32) NOT NULL,
  current_display_user_id_expires_at_ms BIGINT NULL,
  username VARCHAR(64) NULL,
  gender VARCHAR(32) NULL,
  country VARCHAR(64) NULL,
  region_id BIGINT NULL,
  invite_code VARCHAR(64) NULL,
  register_ip VARCHAR(64) NULL,
  register_user_agent VARCHAR(512) NULL,
  country_by_ip VARCHAR(64) NULL,
  register_device_id VARCHAR(128) NULL,
  register_device VARCHAR(128) NULL,
  os_version VARCHAR(64) NULL,
  avatar VARCHAR(512) NULL,
  birth_date DATE NULL,
  app_version VARCHAR(64) NULL,
  build_number VARCHAR(64) NULL,
  source VARCHAR(64) NULL,
  install_channel VARCHAR(64) NULL,
  campaign VARCHAR(128) NULL,
  platform VARCHAR(16) NULL,
  language VARCHAR(32) NULL,
  timezone VARCHAR(64) NULL,
  profile_completed TINYINT(1) NOT NULL DEFAULT 0,
  profile_completed_at_ms BIGINT NOT NULL DEFAULT 0,
  onboarding_status VARCHAR(32) NOT NULL DEFAULT 'profile_required',
  status VARCHAR(32) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_users_default_display_user_id (app_code, default_display_user_id),
  UNIQUE KEY uk_users_current_display_user_id (app_code, current_display_user_id),
  KEY idx_users_status (app_code, status),
  KEY idx_users_region_status (app_code, region_id, status),
  KEY idx_users_country_region (app_code, country, region_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_profile_stats (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  visitors_count BIGINT NOT NULL DEFAULT 0,
  following_count BIGINT NOT NULL DEFAULT 0,
  friends_count BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_profile_visits (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  target_user_id BIGINT NOT NULL,
  visitor_user_id BIGINT NOT NULL,
  visit_count BIGINT NOT NULL DEFAULT 1,
  first_visited_at_ms BIGINT NOT NULL,
  last_visited_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, target_user_id, visitor_user_id),
  KEY idx_user_profile_visits_visitor (app_code, visitor_user_id, last_visited_at_ms),
  KEY idx_user_profile_visits_target_time (app_code, target_user_id, last_visited_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_follows (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  follower_user_id BIGINT NOT NULL,
  followee_user_id BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  followed_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, follower_user_id, followee_user_id),
  KEY idx_user_follows_followee_status (app_code, followee_user_id, status, updated_at_ms),
  KEY idx_user_follows_follower_status (app_code, follower_user_id, status, followed_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_friend_applications (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  requester_user_id BIGINT NOT NULL,
  target_user_id BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, requester_user_id, target_user_id),
  KEY idx_friend_applications_target_status (app_code, target_user_id, status, updated_at_ms),
  KEY idx_friend_applications_requester_status (app_code, requester_user_id, status, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_friendships (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  friend_user_id BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  friended_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id, friend_user_id),
  KEY idx_user_friendships_user_status (app_code, user_id, status, friended_at_ms),
  KEY idx_user_friendships_friend_status (app_code, friend_user_id, status, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_status_change_logs (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  target_user_id BIGINT NOT NULL,
  old_status VARCHAR(32) NOT NULL,
  new_status VARCHAR(32) NOT NULL,
  operator_type VARCHAR(32) NOT NULL,
  operator_user_id BIGINT NOT NULL DEFAULT 0,
  reason VARCHAR(128) NOT NULL DEFAULT '',
  request_id VARCHAR(96) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  KEY idx_user_status_logs_target (app_code, target_user_id, created_at_ms),
  KEY idx_user_status_logs_status (app_code, new_status, created_at_ms),
  KEY idx_user_status_logs_operator (app_code, operator_type, operator_user_id, created_at_ms),
  KEY idx_user_status_logs_request (app_code, request_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS display_user_id_sequences (
  app_code VARCHAR(32) NOT NULL PRIMARY KEY,
  next_value BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO display_user_id_sequences (app_code, next_value, updated_at_ms)
VALUES ('lalu', 163000, 0)
ON DUPLICATE KEY UPDATE
  next_value = next_value;

CREATE TABLE IF NOT EXISTS countries (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  country_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  country_name VARCHAR(128) NOT NULL,
  country_code VARCHAR(3) NOT NULL,
  iso_alpha3 VARCHAR(3) NULL,
  iso_numeric CHAR(3) NULL,
  country_display_name VARCHAR(128) NOT NULL,
  phone_country_code VARCHAR(8) NULL,
  flag VARCHAR(16) NOT NULL DEFAULT '',
  enabled BOOLEAN NOT NULL DEFAULT TRUE,
  sort_order INT NOT NULL DEFAULT 0,
  created_by_user_id BIGINT NULL,
  updated_by_user_id BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_countries_code (app_code, country_code),
  UNIQUE KEY uk_countries_iso_numeric (app_code, iso_numeric),
  KEY idx_countries_phone_country_code (app_code, phone_country_code),
  KEY idx_countries_app_enabled_sort (app_code, enabled, sort_order, country_code),
  KEY idx_countries_sort (app_code, sort_order, country_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS regions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  region_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  region_code VARCHAR(64) NOT NULL,
  name VARCHAR(128) NOT NULL,
  status VARCHAR(32) NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  created_by_user_id BIGINT NULL,
  updated_by_user_id BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_regions_code (app_code, region_code),
  KEY idx_regions_status_sort (app_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS region_countries (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  region_id BIGINT NOT NULL,
  country_code VARCHAR(3) NOT NULL,
  status VARCHAR(32) NOT NULL,
  created_by_user_id BIGINT NULL,
  updated_by_user_id BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  active_country_code VARCHAR(3)
    GENERATED ALWAYS AS (
      CASE WHEN status = 'active' THEN country_code ELSE NULL END
    ) STORED,
  UNIQUE KEY uk_region_countries_active_country (app_code, active_country_code),
  KEY idx_region_countries_country_status (app_code, country_code, status),
  KEY idx_region_countries_region_status (app_code, region_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS region_change_logs (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  region_id BIGINT NULL,
  operation VARCHAR(64) NOT NULL,
  before_json JSON NULL,
  after_json JSON NULL,
  operator_user_id BIGINT NULL,
  request_id VARCHAR(96) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  KEY idx_region_change_logs_region (app_code, region_id, created_at_ms),
  KEY idx_region_change_logs_request_id (app_code, request_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_region_rebuild_tasks (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  task_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  region_id BIGINT NULL,
  country_code VARCHAR(3) NOT NULL,
  target_region_id BIGINT NULL,
  mapping_revision BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  cursor_user_id BIGINT NOT NULL DEFAULT 0,
  locked_by VARCHAR(128) NULL,
  locked_until_ms BIGINT NULL,
  attempt_count INT NOT NULL DEFAULT 0,
  error_message VARCHAR(512) NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  KEY idx_user_region_rebuild_tasks_status (app_code, status, updated_at_ms),
  KEY idx_user_region_rebuild_tasks_country (app_code, country_code, status),
  KEY idx_user_region_rebuild_tasks_lock (app_code, status, locked_until_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_push_tokens (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  device_id VARCHAR(128) NOT NULL,
  push_token VARCHAR(512) NOT NULL,
  provider VARCHAR(32) NOT NULL,
  platform VARCHAR(16) NOT NULL,
  app_version VARCHAR(64) NOT NULL DEFAULT '',
  language VARCHAR(32) NOT NULL DEFAULT '',
  timezone VARCHAR(64) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL,
  bound_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  last_request_id VARCHAR(96) NOT NULL DEFAULT '',
  PRIMARY KEY (app_code, user_id, device_id),
  UNIQUE KEY uk_user_push_tokens_token (app_code, provider, push_token),
  KEY idx_user_push_tokens_user_status (app_code, user_id, status),
  KEY idx_user_push_tokens_updated (app_code, status, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_invite_codes (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  invite_code_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  owner_user_id BIGINT NOT NULL,
  code VARCHAR(64) NOT NULL,
  code_type VARCHAR(32) NOT NULL DEFAULT 'primary',
  status VARCHAR(32) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_user_invite_codes_code (app_code, code),
  UNIQUE KEY uk_user_invite_codes_owner_primary (app_code, owner_user_id, code_type),
  KEY idx_user_invite_codes_owner (app_code, owner_user_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_invite_relations (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  relation_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  invited_user_id BIGINT NOT NULL,
  inviter_user_id BIGINT NOT NULL,
  invite_code_id BIGINT NOT NULL,
  invite_code VARCHAR(64) NOT NULL,
  source VARCHAR(64) NOT NULL,
  request_id VARCHAR(96) NOT NULL DEFAULT '',
  bound_at_ms BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_user_invite_relations_invited (app_code, invited_user_id),
  KEY idx_user_invite_relations_inviter (app_code, inviter_user_id, bound_at_ms),
  KEY idx_user_invite_relations_code (app_code, invite_code, bound_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_invite_validity_policies (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  policy_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  region_id BIGINT NOT NULL DEFAULT 0,
  policy_version VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  required_recharge_coin_amount BIGINT NOT NULL,
  eligible_recharge_types JSON NOT NULL,
  effective_from_ms BIGINT NOT NULL,
  effective_to_ms BIGINT NULL,
  created_by_user_id BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_user_invite_policy_version (app_code, region_id, policy_version),
  KEY idx_user_invite_policies_active (app_code, region_id, status, effective_from_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO user_invite_validity_policies (
  app_code, region_id, policy_version, status, required_recharge_coin_amount,
  eligible_recharge_types, effective_from_ms, effective_to_ms, created_by_user_id, created_at_ms, updated_at_ms
) VALUES (
  'lalu', 0, 'invite-valid-default-v1', 'active', 80000,
  JSON_ARRAY('coin_seller_transfer'), 0, NULL, NULL, 0, 0
) ON DUPLICATE KEY UPDATE
  status = VALUES(status),
  required_recharge_coin_amount = VALUES(required_recharge_coin_amount),
  eligible_recharge_types = VALUES(eligible_recharge_types),
  effective_from_ms = VALUES(effective_from_ms),
  effective_to_ms = VALUES(effective_to_ms),
  updated_at_ms = VALUES(updated_at_ms);

CREATE TABLE IF NOT EXISTS user_invite_recharge_progress (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  invited_user_id BIGINT NOT NULL,
  inviter_user_id BIGINT NOT NULL,
  policy_id BIGINT NOT NULL,
  policy_version VARCHAR(64) NOT NULL,
  required_recharge_coin_amount BIGINT NOT NULL,
  accumulated_recharge_coin_amount BIGINT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL,
  valid_at_ms BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, invited_user_id),
  KEY idx_user_invite_progress_inviter_status (app_code, inviter_user_id, status, updated_at_ms),
  KEY idx_user_invite_progress_policy (app_code, policy_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_invite_event_consumption (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  event_id VARCHAR(128) NOT NULL,
  event_type VARCHAR(64) NOT NULL,
  transaction_id VARCHAR(96) NOT NULL,
  invited_user_id BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  skip_reason VARCHAR(128) NOT NULL DEFAULT '',
  consumed_at_ms BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, event_id),
  UNIQUE KEY uk_user_invite_event_tx (app_code, transaction_id, event_type),
  KEY idx_user_invite_event_user (app_code, invited_user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_invite_counters (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  invite_count BIGINT NOT NULL DEFAULT 0,
  valid_invite_count BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_outbox (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  event_id VARCHAR(128) NOT NULL,
  event_type VARCHAR(64) NOT NULL,
  aggregate_type VARCHAR(64) NOT NULL,
  aggregate_id BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  payload_json JSON NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, event_id),
  KEY idx_user_outbox_status_created (app_code, status, created_at_ms),
  KEY idx_user_outbox_aggregate (app_code, aggregate_type, aggregate_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_mic_sessions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  mic_session_id VARCHAR(128) NOT NULL,
  user_id BIGINT NOT NULL,
  room_id VARCHAR(64) NOT NULL,
  first_seat_no INT NOT NULL,
  current_seat_no INT NOT NULL,
  status VARCHAR(32) NOT NULL,
  seat_started_at_ms BIGINT NOT NULL,
  publishing_started_at_ms BIGINT NULL,
  ended_at_ms BIGINT NULL,
  end_reason VARCHAR(64) NOT NULL DEFAULT '',
  seat_occupied_ms BIGINT NOT NULL DEFAULT 0,
  mic_online_ms BIGINT NOT NULL DEFAULT 0,
  opened_event_id VARCHAR(128) NOT NULL,
  publish_event_id VARCHAR(128) NOT NULL DEFAULT '',
  closed_event_id VARCHAR(128) NOT NULL DEFAULT '',
  last_event_id VARCHAR(128) NOT NULL,
  last_room_version BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, mic_session_id),
  KEY idx_user_mic_sessions_user_status (app_code, user_id, status, updated_at_ms),
  KEY idx_user_mic_sessions_room_status (app_code, room_id, status),
  KEY idx_user_mic_sessions_open_timeout (app_code, status, seat_started_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_mic_daily_stats (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  stat_date CHAR(10) NOT NULL,
  seat_occupied_ms BIGINT NOT NULL DEFAULT 0,
  mic_online_ms BIGINT NOT NULL DEFAULT 0,
  session_count BIGINT NOT NULL DEFAULT 0,
  room_count BIGINT NOT NULL DEFAULT 0,
  first_mic_at_ms BIGINT NOT NULL DEFAULT 0,
  last_mic_at_ms BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id, stat_date),
  KEY idx_user_mic_daily_date (app_code, stat_date, mic_online_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_mic_lifetime_stats (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  seat_occupied_ms BIGINT NOT NULL DEFAULT 0,
  mic_online_ms BIGINT NOT NULL DEFAULT 0,
  session_count BIGINT NOT NULL DEFAULT 0,
  first_mic_at_ms BIGINT NOT NULL DEFAULT 0,
  last_mic_at_ms BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id),
  KEY idx_user_mic_lifetime_mic_online (app_code, mic_online_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_mic_event_consumption (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  event_id VARCHAR(128) NOT NULL,
  event_type VARCHAR(64) NOT NULL,
  mic_session_id VARCHAR(128) NOT NULL,
  user_id BIGINT NOT NULL,
  action VARCHAR(32) NOT NULL,
  status VARCHAR(32) NOT NULL,
  skip_reason VARCHAR(128) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  consumed_at_ms BIGINT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, event_id),
  KEY idx_user_mic_event_session (app_code, mic_session_id, created_at_ms),
  KEY idx_user_mic_event_user (app_code, user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS host_profiles (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL PRIMARY KEY,
  status VARCHAR(32) NOT NULL,
  region_id BIGINT NOT NULL,
  current_agency_id BIGINT NULL,
  current_membership_id BIGINT NULL,
  source VARCHAR(64) NOT NULL,
  first_became_host_at_ms BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  KEY idx_host_profiles_region_status (app_code, region_id, status),
  KEY idx_host_profiles_agency (app_code, current_agency_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS bd_profiles (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL PRIMARY KEY,
  role VARCHAR(32) NOT NULL,
  region_id BIGINT NOT NULL,
  parent_leader_user_id BIGINT NULL,
  status VARCHAR(32) NOT NULL,
  created_by_user_id BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  KEY idx_bd_profiles_region_role (app_code, region_id, role, status),
  KEY idx_bd_profiles_parent_leader (app_code, parent_leader_user_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS coin_seller_profiles (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL PRIMARY KEY,
  status VARCHAR(32) NOT NULL,
  merchant_asset_type VARCHAR(32) NOT NULL DEFAULT 'COIN_SELLER_COIN',
  contact_info VARCHAR(128) NOT NULL DEFAULT '',
  created_by_user_id BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  KEY idx_coin_seller_profiles_status (app_code, status, updated_at_ms),
  KEY idx_coin_seller_profiles_created_by (app_code, created_by_user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS agencies (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  agency_id BIGINT NOT NULL PRIMARY KEY,
  owner_user_id BIGINT NOT NULL,
  region_id BIGINT NOT NULL,
  parent_bd_user_id BIGINT NOT NULL,
  name VARCHAR(128) NOT NULL,
  status VARCHAR(32) NOT NULL,
  join_enabled BOOLEAN NOT NULL,
  max_hosts INT NOT NULL,
  created_by_user_id BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  active_owner_user_id BIGINT GENERATED ALWAYS AS (
    CASE WHEN status = 'active' THEN owner_user_id ELSE NULL END
  ) STORED,
  UNIQUE KEY uk_agencies_active_owner (app_code, active_owner_user_id),
  KEY idx_agencies_region_status (app_code, region_id, status, join_enabled),
  KEY idx_agencies_parent_bd (app_code, parent_bd_user_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS agency_memberships (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  membership_id BIGINT NOT NULL PRIMARY KEY,
  agency_id BIGINT NOT NULL,
  host_user_id BIGINT NOT NULL,
  region_id BIGINT NOT NULL,
  membership_type VARCHAR(32) NOT NULL,
  status VARCHAR(32) NOT NULL,
  joined_at_ms BIGINT NOT NULL,
  ended_at_ms BIGINT NULL,
  ended_by_user_id BIGINT NULL,
  ended_reason VARCHAR(64) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  active_host_user_id BIGINT GENERATED ALWAYS AS (
    CASE WHEN status = 'active' THEN host_user_id ELSE NULL END
  ) STORED,
  UNIQUE KEY uk_agency_memberships_active_host (app_code, active_host_user_id),
  KEY idx_agency_memberships_agency_status (app_code, agency_id, status),
  KEY idx_agency_memberships_host_status (app_code, host_user_id, status),
  KEY idx_agency_memberships_effective (app_code, host_user_id, joined_at_ms, ended_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS agency_applications (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  application_id BIGINT NOT NULL PRIMARY KEY,
  command_id VARCHAR(96) NOT NULL,
  applicant_user_id BIGINT NOT NULL,
  agency_id BIGINT NOT NULL,
  region_id BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  reviewed_by_user_id BIGINT NULL,
  review_reason VARCHAR(512) NOT NULL DEFAULT '',
  reviewed_at_ms BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  pending_applicant_user_id BIGINT GENERATED ALWAYS AS (
    CASE WHEN status = 'pending' THEN applicant_user_id ELSE NULL END
  ) STORED,
  UNIQUE KEY uk_agency_applications_command (app_code, command_id),
  UNIQUE KEY uk_agency_applications_pending_user (app_code, pending_applicant_user_id),
  KEY idx_agency_applications_agency_status (app_code, agency_id, status, created_at_ms),
  KEY idx_agency_applications_applicant_status (app_code, applicant_user_id, status, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS role_invitations (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  invitation_id BIGINT NOT NULL PRIMARY KEY,
  command_id VARCHAR(96) NOT NULL,
  invitation_type VARCHAR(32) NOT NULL,
  status VARCHAR(32) NOT NULL,
  inviter_user_id BIGINT NOT NULL,
  inviter_bd_user_id BIGINT NOT NULL,
  target_user_id BIGINT NOT NULL,
  region_id BIGINT NOT NULL,
  agency_name VARCHAR(128) NOT NULL DEFAULT '',
  parent_bd_user_id BIGINT NOT NULL DEFAULT 0,
  parent_leader_user_id BIGINT NULL,
  processed_by_user_id BIGINT NULL,
  process_reason VARCHAR(512) NOT NULL DEFAULT '',
  processed_at_ms BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  pending_target_key VARCHAR(128) GENERATED ALWAYS AS (
    CASE WHEN status = 'pending' THEN CONCAT(invitation_type, ':', target_user_id) ELSE NULL END
  ) STORED,
  UNIQUE KEY uk_role_invitations_command (app_code, command_id),
  UNIQUE KEY uk_role_invitations_pending_target (app_code, pending_target_key),
  KEY idx_role_invitations_target_status (app_code, target_user_id, status, created_at_ms),
  KEY idx_role_invitations_inviter_status (app_code, inviter_user_id, status, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS host_command_results (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  command_id VARCHAR(96) NOT NULL PRIMARY KEY,
  command_type VARCHAR(64) NOT NULL,
  result_type VARCHAR(64) NOT NULL,
  result_id BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  KEY idx_host_command_results_result (app_code, result_type, result_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


CREATE TABLE IF NOT EXISTS host_outbox (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  event_id BIGINT NOT NULL PRIMARY KEY,
  event_type VARCHAR(64) NOT NULL,
  aggregate_type VARCHAR(64) NOT NULL,
  aggregate_id VARCHAR(96) NOT NULL,
  status VARCHAR(32) NOT NULL,
  payload_json JSON NOT NULL,
  created_at_ms BIGINT NOT NULL,
  retry_count INT NOT NULL DEFAULT 0,
  last_error VARCHAR(512) NOT NULL DEFAULT '',
  KEY idx_host_outbox_status_created (app_code, status, created_at_ms),
  KEY idx_host_outbox_aggregate (app_code, aggregate_type, aggregate_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


-- Country/region bootstrap is generated from services/user-service/deploy/data/countries.json.
-- region_code intentionally uses source subregion directly; top-level region is not imported.
INSERT INTO countries (country_name, country_code, iso_alpha3, iso_numeric, country_display_name, phone_country_code, flag, enabled, sort_order, created_at_ms, updated_at_ms) VALUES
  ('Andorra', 'AD', 'AND', '020', '安道尔', '+376', '🇦🇩', TRUE, 10, 0, 0),
  ('United Arab Emirates', 'AE', 'ARE', '784', '阿拉伯联合酋长国', '+971', '🇦🇪', TRUE, 20, 0, 0),
  ('Afghanistan', 'AF', 'AFG', '004', '阿富汗', '+93', '🇦🇫', TRUE, 30, 0, 0),
  ('Anguilla', 'AI', 'AIA', '660', '安圭拉', '+1', '🇦🇮', TRUE, 50, 0, 0),
  ('Albania', 'AL', 'ALB', '008', '阿尔巴尼亚', '+355', '🇦🇱', TRUE, 60, 0, 0),
  ('Armenia', 'AM', 'ARM', '051', '亚美尼亚', '+374', '🇦🇲', TRUE, 70, 0, 0),
  ('Angola', 'AO', 'AGO', '024', '安哥拉', '+244', '🇦🇴', TRUE, 80, 0, 0),
  ('Argentina', 'AR', 'ARG', '032', '阿根廷', '+54', '🇦🇷', TRUE, 100, 0, 0),
  ('Austria', 'AT', 'AUT', '040', '奥地利', '+43', '🇦🇹', TRUE, 120, 0, 0),
  ('Australia', 'AU', 'AUS', '036', '澳大利亚', '+61', '🇦🇺', TRUE, 130, 0, 0),
  ('Aruba', 'AW', 'ABW', '533', '阿鲁巴', '+297', '🇦🇼', TRUE, 140, 0, 0),
  ('Åland Islands', 'AX', 'ALA', '248', '奥兰群岛', '+358', '🇦🇽', TRUE, 150, 0, 0),
  ('Azerbaijan', 'AZ', 'AZE', '031', '阿塞拜疆', '+994', '🇦🇿', TRUE, 160, 0, 0),
  ('Barbados', 'BB', 'BRB', '052', '巴巴多斯', '+1', '🇧🇧', TRUE, 180, 0, 0),
  ('Bangladesh', 'BD', 'BGD', '050', '孟加拉国', '+880', '🇧🇩', TRUE, 190, 0, 0),
  ('Belgium', 'BE', 'BEL', '056', '比利时', '+32', '🇧🇪', TRUE, 200, 0, 0),
  ('Burkina Faso', 'BF', 'BFA', '854', '布基纳法索', '+226', '🇧🇫', TRUE, 210, 0, 0),
  ('Bulgaria', 'BG', 'BGR', '100', '保加利亚', '+359', '🇧🇬', TRUE, 220, 0, 0),
  ('Bahrain', 'BH', 'BHR', '048', '巴林', '+973', '🇧🇭', TRUE, 230, 0, 0),
  ('Burundi', 'BI', 'BDI', '108', '布隆迪', '+257', '🇧🇮', TRUE, 240, 0, 0),
  ('Benin', 'BJ', 'BEN', '204', '贝宁', '+229', '🇧🇯', TRUE, 250, 0, 0),
  ('Saint Barthélemy', 'BL', 'BLM', '652', '圣巴泰勒米', '+590', '🇧🇱', TRUE, 260, 0, 0),
  ('Bermuda', 'BM', 'BMU', '060', '百慕大', '+1', '🇧🇲', TRUE, 270, 0, 0),
  ('Brunei', 'BN', 'BRN', '096', '文莱', '+673', '🇧🇳', TRUE, 280, 0, 0),
  ('Bolivia', 'BO', 'BOL', '068', '玻利维亚', '+591', '🇧🇴', TRUE, 290, 0, 0),
  ('Brazil', 'BR', 'BRA', '076', '巴西', '+55', '🇧🇷', TRUE, 310, 0, 0),
  ('Bahamas', 'BS', 'BHS', '044', '巴哈马', '+1', '🇧🇸', TRUE, 320, 0, 0),
  ('Bhutan', 'BT', 'BTN', '064', '不丹', '+975', '🇧🇹', TRUE, 330, 0, 0),
  ('Bouvet Island', 'BV', 'BVT', '074', '布维岛', NULL, '🇧🇻', TRUE, 340, 0, 0),
  ('Botswana', 'BW', 'BWA', '072', '博茨瓦纳', '+267', '🇧🇼', TRUE, 350, 0, 0),
  ('Belarus', 'BY', 'BLR', '112', '白俄罗斯', '+375', '🇧🇾', TRUE, 360, 0, 0),
  ('Belize', 'BZ', 'BLZ', '084', '伯利兹', '+501', '🇧🇿', TRUE, 370, 0, 0),
  ('Canada', 'CA', 'CAN', '124', '加拿大', '+1', '🇨🇦', TRUE, 380, 0, 0),
  ('Cocos (Keeling) Islands', 'CC', 'CCK', '166', '科科斯', '+61', '🇨🇨', TRUE, 390, 0, 0),
  ('DR Congo', 'CD', 'COD', '180', '民主刚果', '+243', '🇨🇩', TRUE, 400, 0, 0),
  ('Central African Republic', 'CF', 'CAF', '140', '中非共和国', '+236', '🇨🇫', TRUE, 410, 0, 0),
  ('Congo', 'CG', 'COG', '178', '刚果', '+242', '🇨🇬', TRUE, 420, 0, 0),
  ('Switzerland', 'CH', 'CHE', '756', '瑞士', '+41', '🇨🇭', TRUE, 430, 0, 0),
  ('Ivory Coast', 'CI', 'CIV', '384', '科特迪瓦', '+225', '🇨🇮', TRUE, 440, 0, 0),
  ('Chile', 'CL', 'CHL', '152', '智利', '+56', '🇨🇱', TRUE, 460, 0, 0),
  ('Cameroon', 'CM', 'CMR', '120', '喀麦隆', '+237', '🇨🇲', TRUE, 470, 0, 0),
  ('China', 'CN', 'CHN', '156', '中国', '+86', '🇨🇳', TRUE, 480, 0, 0),
  ('Colombia', 'CO', 'COL', '170', '哥伦比亚', '+57', '🇨🇴', TRUE, 490, 0, 0),
  ('Costa Rica', 'CR', 'CRI', '188', '哥斯达黎加', '+506', '🇨🇷', TRUE, 500, 0, 0),
  ('Cuba', 'CU', 'CUB', '192', '古巴', '+53', '🇨🇺', TRUE, 510, 0, 0),
  ('Cape Verde', 'CV', 'CPV', '132', '佛得角', '+238', '🇨🇻', TRUE, 520, 0, 0),
  ('Curaçao', 'CW', 'CUW', '531', '库拉索', '+599', '🇨🇼', TRUE, 530, 0, 0),
  ('Christmas Island', 'CX', 'CXR', '162', '圣诞岛', '+61', '🇨🇽', TRUE, 540, 0, 0),
  ('Cyprus', 'CY', 'CYP', '196', '塞浦路斯', '+357', '🇨🇾', TRUE, 550, 0, 0),
  ('Czechia', 'CZ', 'CZE', '203', '捷克', '+420', '🇨🇿', TRUE, 560, 0, 0),
  ('Germany', 'DE', 'DEU', '276', '德国', '+49', '🇩🇪', TRUE, 570, 0, 0),
  ('Djibouti', 'DJ', 'DJI', '262', '吉布提', '+253', '🇩🇯', TRUE, 580, 0, 0),
  ('Denmark', 'DK', 'DNK', '208', '丹麦', '+45', '🇩🇰', TRUE, 590, 0, 0),
  ('Dominica', 'DM', 'DMA', '212', '多米尼加', '+1', '🇩🇲', TRUE, 600, 0, 0),
  ('Dominican Republic', 'DO', 'DOM', '214', '多明尼加', '+1', '🇩🇴', TRUE, 610, 0, 0),
  ('Algeria', 'DZ', 'DZA', '012', '阿尔及利亚', '+213', '🇩🇿', TRUE, 620, 0, 0),
  ('Ecuador', 'EC', 'ECU', '218', '厄瓜多尔', '+593', '🇪🇨', TRUE, 630, 0, 0),
  ('Estonia', 'EE', 'EST', '233', '爱沙尼亚', '+372', '🇪🇪', TRUE, 640, 0, 0),
  ('Egypt', 'EG', 'EGY', '818', '埃及', '+20', '🇪🇬', TRUE, 650, 0, 0),
  ('Eritrea', 'ER', 'ERI', '232', '厄立特里亚', '+291', '🇪🇷', TRUE, 670, 0, 0),
  ('Spain', 'ES', 'ESP', '724', '西班牙', '+34', '🇪🇸', TRUE, 680, 0, 0),
  ('Ethiopia', 'ET', 'ETH', '231', '埃塞俄比亚', '+251', '🇪🇹', TRUE, 690, 0, 0),
  ('Finland', 'FI', 'FIN', '246', '芬兰', '+358', '🇫🇮', TRUE, 700, 0, 0),
  ('Fiji', 'FJ', 'FJI', '242', '斐济', '+679', '🇫🇯', TRUE, 710, 0, 0),
  ('Micronesia', 'FM', 'FSM', '583', '密克罗尼西亚', '+691', '🇫🇲', TRUE, 730, 0, 0),
  ('France', 'FR', 'FRA', '250', '法国', '+33', '🇫🇷', TRUE, 750, 0, 0),
  ('Gabon', 'GA', 'GAB', '266', '加蓬', '+241', '🇬🇦', TRUE, 760, 0, 0),
  ('United Kingdom', 'GB', 'GBR', '826', '英国', '+44', '🇬🇧', TRUE, 770, 0, 0),
  ('Grenada', 'GD', 'GRD', '308', '格林纳达', '+1', '🇬🇩', TRUE, 780, 0, 0),
  ('Georgia', 'GE', 'GEO', '268', '格鲁吉亚', '+995', '🇬🇪', TRUE, 790, 0, 0),
  ('French Guiana', 'GF', 'GUF', '254', '法属圭亚那', '+594', '🇬🇫', TRUE, 800, 0, 0),
  ('Guernsey', 'GG', 'GGY', '831', '根西岛', '+44', '🇬🇬', TRUE, 810, 0, 0),
  ('Ghana', 'GH', 'GHA', '288', '加纳', '+233', '🇬🇭', TRUE, 820, 0, 0),
  ('Gibraltar', 'GI', 'GIB', '292', '直布罗陀', '+350', '🇬🇮', TRUE, 830, 0, 0),
  ('Greenland', 'GL', 'GRL', '304', '格陵兰', '+299', '🇬🇱', TRUE, 840, 0, 0),
  ('Gambia', 'GM', 'GMB', '270', '冈比亚', '+220', '🇬🇲', TRUE, 850, 0, 0),
  ('Guinea', 'GN', 'GIN', '324', '几内亚', '+224', '🇬🇳', TRUE, 860, 0, 0),
  ('Equatorial Guinea', 'GQ', 'GNQ', '226', '赤道几内亚', '+240', '🇬🇶', TRUE, 880, 0, 0),
  ('Greece', 'GR', 'GRC', '300', '希腊', '+30', '🇬🇷', TRUE, 890, 0, 0),
  ('South Georgia', 'GS', 'SGS', '239', '南乔治亚', NULL, '🇬🇸', TRUE, 900, 0, 0),
  ('Guatemala', 'GT', 'GTM', '320', '危地马拉', '+502', '🇬🇹', TRUE, 910, 0, 0),
  ('Guam', 'GU', 'GUM', '316', '关岛', '+1', '🇬🇺', TRUE, 920, 0, 0),
  ('Guyana', 'GY', 'GUY', '328', '圭亚那', '+592', '🇬🇾', TRUE, 940, 0, 0),
  ('Honduras', 'HN', 'HND', '340', '洪都拉斯', '+504', '🇭🇳', TRUE, 970, 0, 0),
  ('Croatia', 'HR', 'HRV', '191', '克罗地亚', '+385', '🇭🇷', TRUE, 980, 0, 0),
  ('Haiti', 'HT', 'HTI', '332', '海地', '+509', '🇭🇹', TRUE, 990, 0, 0),
  ('Hungary', 'HU', 'HUN', '348', '匈牙利', '+36', '🇭🇺', TRUE, 1000, 0, 0),
  ('Indonesia', 'ID', 'IDN', '360', '印度尼西亚', '+62', '🇮🇩', TRUE, 1010, 0, 0),
  ('Ireland', 'IE', 'IRL', '372', '爱尔兰', '+353', '🇮🇪', TRUE, 1020, 0, 0),
  ('Israel', 'IL', 'ISR', '376', '以色列', '+972', '🇮🇱', TRUE, 1030, 0, 0),
  ('Isle of Man', 'IM', 'IMN', '833', '马恩岛', '+44', '🇮🇲', TRUE, 1040, 0, 0),
  ('India', 'IN', 'IND', '356', '印度', '+91', '🇮🇳', TRUE, 1050, 0, 0),
  ('Iraq', 'IQ', 'IRQ', '368', '伊拉克', '+964', '🇮🇶', TRUE, 1070, 0, 0),
  ('Iran', 'IR', 'IRN', '364', '伊朗', '+98', '🇮🇷', TRUE, 1080, 0, 0),
  ('Iceland', 'IS', 'ISL', '352', '冰岛', '+354', '🇮🇸', TRUE, 1090, 0, 0),
  ('Italy', 'IT', 'ITA', '380', '意大利', '+39', '🇮🇹', TRUE, 1100, 0, 0),
  ('Jersey', 'JE', 'JEY', '832', '泽西岛', '+44', '🇯🇪', TRUE, 1110, 0, 0),
  ('Jamaica', 'JM', 'JAM', '388', '牙买加', '+1', '🇯🇲', TRUE, 1120, 0, 0),
  ('Jordan', 'JO', 'JOR', '400', '约旦', '+962', '🇯🇴', TRUE, 1130, 0, 0),
  ('Japan', 'JP', 'JPN', '392', '日本', '+81', '🇯🇵', TRUE, 1140, 0, 0),
  ('Kenya', 'KE', 'KEN', '404', '肯尼亚', '+254', '🇰🇪', TRUE, 1150, 0, 0),
  ('Kyrgyzstan', 'KG', 'KGZ', '417', '吉尔吉斯斯坦', '+996', '🇰🇬', TRUE, 1160, 0, 0),
  ('Cambodia', 'KH', 'KHM', '116', '柬埔寨', '+855', '🇰🇭', TRUE, 1170, 0, 0),
  ('Kiribati', 'KI', 'KIR', '296', '基里巴斯', '+686', '🇰🇮', TRUE, 1180, 0, 0),
  ('Comoros', 'KM', 'COM', '174', '科摩罗', '+269', '🇰🇲', TRUE, 1190, 0, 0),
  ('North Korea', 'KP', 'PRK', '408', '朝鲜', '+850', '🇰🇵', TRUE, 1210, 0, 0),
  ('South Korea', 'KR', 'KOR', '410', '韩国', '+82', '🇰🇷', TRUE, 1220, 0, 0),
  ('Kuwait', 'KW', 'KWT', '414', '科威特', '+965', '🇰🇼', TRUE, 1230, 0, 0),
  ('Cayman Islands', 'KY', 'CYM', '136', '开曼群岛', '+1', '🇰🇾', TRUE, 1240, 0, 0),
  ('Kazakhstan', 'KZ', 'KAZ', '398', '哈萨克斯坦', '+7', '🇰🇿', TRUE, 1250, 0, 0),
  ('Laos', 'LA', 'LAO', '418', '老挝', '+856', '🇱🇦', TRUE, 1260, 0, 0),
  ('Lebanon', 'LB', 'LBN', '422', '黎巴嫩', '+961', '🇱🇧', TRUE, 1270, 0, 0),
  ('Saint Lucia', 'LC', 'LCA', '662', '圣卢西亚', '+1', '🇱🇨', TRUE, 1280, 0, 0),
  ('Liechtenstein', 'LI', 'LIE', '438', '列支敦士登', '+423', '🇱🇮', TRUE, 1290, 0, 0),
  ('Sri Lanka', 'LK', 'LKA', '144', '斯里兰卡', '+94', '🇱🇰', TRUE, 1300, 0, 0),
  ('Liberia', 'LR', 'LBR', '430', '利比里亚', '+231', '🇱🇷', TRUE, 1310, 0, 0),
  ('Lesotho', 'LS', 'LSO', '426', '莱索托', '+266', '🇱🇸', TRUE, 1320, 0, 0),
  ('Lithuania', 'LT', 'LTU', '440', '立陶宛', '+370', '🇱🇹', TRUE, 1330, 0, 0),
  ('Luxembourg', 'LU', 'LUX', '442', '卢森堡', '+352', '🇱🇺', TRUE, 1340, 0, 0),
  ('Latvia', 'LV', 'LVA', '428', '拉脱维亚', '+371', '🇱🇻', TRUE, 1350, 0, 0),
  ('Libya', 'LY', 'LBY', '434', '利比亚', '+218', '🇱🇾', TRUE, 1360, 0, 0),
  ('Morocco', 'MA', 'MAR', '504', '摩洛哥', '+212', '🇲🇦', TRUE, 1370, 0, 0),
  ('Monaco', 'MC', 'MCO', '492', '摩纳哥', '+377', '🇲🇨', TRUE, 1380, 0, 0),
  ('Moldova', 'MD', 'MDA', '498', '摩尔多瓦', '+373', '🇲🇩', TRUE, 1390, 0, 0),
  ('Montenegro', 'ME', 'MNE', '499', '黑山', '+382', '🇲🇪', TRUE, 1400, 0, 0),
  ('Saint Martin', 'MF', 'MAF', '663', '圣马丁', '+590', '🇲🇫', TRUE, 1410, 0, 0),
  ('Madagascar', 'MG', 'MDG', '450', '马达加斯加', '+261', '🇲🇬', TRUE, 1420, 0, 0),
  ('Mali', 'ML', 'MLI', '466', '马里', '+223', '🇲🇱', TRUE, 1450, 0, 0),
  ('Myanmar', 'MM', 'MMR', '104', '缅甸', '+95', '🇲🇲', TRUE, 1460, 0, 0),
  ('Mongolia', 'MN', 'MNG', '496', '蒙古', '+976', '🇲🇳', TRUE, 1470, 0, 0),
  ('Macao', 'MO', 'MAC', '446', '中国澳门', '+853', '🇲🇴', TRUE, 1480, 0, 0),
  ('Martinique', 'MQ', 'MTQ', '474', '马提尼克', '+596', '🇲🇶', TRUE, 1500, 0, 0),
  ('Mauritania', 'MR', 'MRT', '478', '毛里塔尼亚', '+222', '🇲🇷', TRUE, 1510, 0, 0),
  ('Montserrat', 'MS', 'MSR', '500', '蒙特塞拉特', '+1', '🇲🇸', TRUE, 1520, 0, 0),
  ('Malta', 'MT', 'MLT', '470', '马耳他', '+356', '🇲🇹', TRUE, 1530, 0, 0),
  ('Mauritius', 'MU', 'MUS', '480', '毛里求斯', '+230', '🇲🇺', TRUE, 1540, 0, 0),
  ('Maldives', 'MV', 'MDV', '462', '马尔代夫', '+960', '🇲🇻', TRUE, 1550, 0, 0),
  ('Malawi', 'MW', 'MWI', '454', '马拉维', '+265', '🇲🇼', TRUE, 1560, 0, 0),
  ('Mexico', 'MX', 'MEX', '484', '墨西哥', '+52', '🇲🇽', TRUE, 1570, 0, 0),
  ('Malaysia', 'MY', 'MYS', '458', '马来西亚', '+60', '🇲🇾', TRUE, 1580, 0, 0),
  ('Mozambique', 'MZ', 'MOZ', '508', '莫桑比克', '+258', '🇲🇿', TRUE, 1590, 0, 0),
  ('Namibia', 'NA', 'NAM', '516', '纳米比亚', '+264', '🇳🇦', TRUE, 1600, 0, 0),
  ('New Caledonia', 'NC', 'NCL', '540', '新喀里多尼亚', '+687', '🇳🇨', TRUE, 1610, 0, 0),
  ('Niger', 'NE', 'NER', '562', '尼日尔', '+227', '🇳🇪', TRUE, 1620, 0, 0),
  ('Norfolk Island', 'NF', 'NFK', '574', '诺福克岛', '+672', '🇳🇫', TRUE, 1630, 0, 0),
  ('Nigeria', 'NG', 'NGA', '566', '尼日利亚', '+234', '🇳🇬', TRUE, 1640, 0, 0),
  ('Nicaragua', 'NI', 'NIC', '558', '尼加拉瓜', '+505', '🇳🇮', TRUE, 1650, 0, 0),
  ('Netherlands', 'NL', 'NLD', '528', '荷兰', '+31', '🇳🇱', TRUE, 1660, 0, 0),
  ('Norway', 'NO', 'NOR', '578', '挪威', '+47', '🇳🇴', TRUE, 1670, 0, 0),
  ('Nepal', 'NP', 'NPL', '524', '尼泊尔', '+977', '🇳🇵', TRUE, 1680, 0, 0),
  ('Nauru', 'NR', 'NRU', '520', '瑙鲁', '+674', '🇳🇷', TRUE, 1690, 0, 0),
  ('Niue', 'NU', 'NIU', '570', '纽埃', '+683', '🇳🇺', TRUE, 1700, 0, 0),
  ('New Zealand', 'NZ', 'NZL', '554', '新西兰', '+64', '🇳🇿', TRUE, 1710, 0, 0),
  ('Oman', 'OM', 'OMN', '512', '阿曼', '+968', '🇴🇲', TRUE, 1720, 0, 0),
  ('Panama', 'PA', 'PAN', '591', '巴拿马', '+507', '🇵🇦', TRUE, 1730, 0, 0),
  ('Peru', 'PE', 'PER', '604', '秘鲁', '+51', '🇵🇪', TRUE, 1740, 0, 0),
  ('Philippines', 'PH', 'PHL', '608', '菲律宾', '+63', '🇵🇭', TRUE, 1770, 0, 0),
  ('Pakistan', 'PK', 'PAK', '586', '巴基斯坦', '+92', '🇵🇰', TRUE, 1780, 0, 0),
  ('Poland', 'PL', 'POL', '616', '波兰', '+48', '🇵🇱', TRUE, 1790, 0, 0),
  ('Puerto Rico', 'PR', 'PRI', '630', '波多黎各', '+1', '🇵🇷', TRUE, 1820, 0, 0),
  ('Palestine', 'PS', 'PSE', '275', '巴勒斯坦', '+970', '🇵🇸', TRUE, 1830, 0, 0),
  ('Portugal', 'PT', 'PRT', '620', '葡萄牙', '+351', '🇵🇹', TRUE, 1840, 0, 0),
  ('Palau', 'PW', 'PLW', '585', '帕劳', '+680', '🇵🇼', TRUE, 1850, 0, 0),
  ('Paraguay', 'PY', 'PRY', '600', '巴拉圭', '+595', '🇵🇾', TRUE, 1860, 0, 0),
  ('Qatar', 'QA', 'QAT', '634', '卡塔尔', '+974', '🇶🇦', TRUE, 1870, 0, 0),
  ('Réunion', 'RE', 'REU', '638', '留尼旺岛', '+262', '🇷🇪', TRUE, 1880, 0, 0),
  ('Romania', 'RO', 'ROU', '642', '罗马尼亚', '+40', '🇷🇴', TRUE, 1890, 0, 0),
  ('Serbia', 'RS', 'SRB', '688', '塞尔维亚', '+381', '🇷🇸', TRUE, 1900, 0, 0),
  ('Russia', 'RU', 'RUS', '643', '俄罗斯', '+7', '🇷🇺', TRUE, 1910, 0, 0),
  ('Rwanda', 'RW', 'RWA', '646', '卢旺达', '+250', '🇷🇼', TRUE, 1920, 0, 0),
  ('Saudi Arabia', 'SA', 'SAU', '682', '沙特阿拉伯', '+966', '🇸🇦', TRUE, 1930, 0, 0),
  ('Solomon Islands', 'SB', 'SLB', '090', '所罗门群岛', '+677', '🇸🇧', TRUE, 1940, 0, 0),
  ('Seychelles', 'SC', 'SYC', '690', '塞舌尔', '+248', '🇸🇨', TRUE, 1950, 0, 0),
  ('Sudan', 'SD', 'SDN', '729', '苏丹', '+249', '🇸🇩', TRUE, 1960, 0, 0),
  ('Sweden', 'SE', 'SWE', '752', '瑞典', '+46', '🇸🇪', TRUE, 1970, 0, 0),
  ('Singapore', 'SG', 'SGP', '702', '新加坡', '+65', '🇸🇬', TRUE, 1980, 0, 0),
  ('Slovenia', 'SI', 'SVN', '705', '斯洛文尼亚', '+386', '🇸🇮', TRUE, 2000, 0, 0),
  ('Svalbard and Jan Mayen', 'SJ', 'SJM', '744', '斯瓦尔巴特', '+47', '🇸🇯', TRUE, 2010, 0, 0),
  ('Slovakia', 'SK', 'SVK', '703', '斯洛伐克', '+421', '🇸🇰', TRUE, 2020, 0, 0),
  ('Sierra Leone', 'SL', 'SLE', '694', '塞拉利昂', '+232', '🇸🇱', TRUE, 2030, 0, 0),
  ('San Marino', 'SM', 'SMR', '674', '圣马力诺', '+378', '🇸🇲', TRUE, 2040, 0, 0),
  ('Senegal', 'SN', 'SEN', '686', '塞内加尔', '+221', '🇸🇳', TRUE, 2050, 0, 0),
  ('Somalia', 'SO', 'SOM', '706', '索马里', '+252', '🇸🇴', TRUE, 2060, 0, 0),
  ('Suriname', 'SR', 'SUR', '740', '苏里南', '+597', '🇸🇷', TRUE, 2070, 0, 0),
  ('South Sudan', 'SS', 'SSD', '728', '南苏丹', '+211', '🇸🇸', TRUE, 2080, 0, 0),
  ('El Salvador', 'SV', 'SLV', '222', '萨尔瓦多', '+503', '🇸🇻', TRUE, 2100, 0, 0),
  ('Sint Maarten', 'SX', 'SXM', '534', '圣马丁岛', '+1', '🇸🇽', TRUE, 2110, 0, 0),
  ('Syria', 'SY', 'SYR', '760', '叙利亚', '+963', '🇸🇾', TRUE, 2120, 0, 0),
  ('Eswatini', 'SZ', 'SWZ', '748', '斯威士兰', '+268', '🇸🇿', TRUE, 2130, 0, 0),
  ('Chad', 'TD', 'TCD', '148', '乍得', '+235', '🇹🇩', TRUE, 2150, 0, 0),
  ('Togo', 'TG', 'TGO', '768', '多哥', '+228', '🇹🇬', TRUE, 2170, 0, 0),
  ('Thailand', 'TH', 'THA', '764', '泰国', '+66', '🇹🇭', TRUE, 2180, 0, 0),
  ('Tajikistan', 'TJ', 'TJK', '762', '塔吉克斯坦', '+992', '🇹🇯', TRUE, 2190, 0, 0),
  ('Tokelau', 'TK', 'TKL', '772', '托克劳', '+690', '🇹🇰', TRUE, 2200, 0, 0),
  ('Timor-Leste', 'TL', 'TLS', '626', '东帝汶', '+670', '🇹🇱', TRUE, 2210, 0, 0),
  ('Turkmenistan', 'TM', 'TKM', '795', '土库曼斯坦', '+993', '🇹🇲', TRUE, 2220, 0, 0),
  ('Tunisia', 'TN', 'TUN', '788', '突尼斯', '+216', '🇹🇳', TRUE, 2230, 0, 0),
  ('Tonga', 'TO', 'TON', '776', '汤加', '+676', '🇹🇴', TRUE, 2240, 0, 0),
  ('Türkiye', 'TR', 'TUR', '792', '土耳其', '+90', '🇹🇷', TRUE, 2250, 0, 0),
  ('Tuvalu', 'TV', 'TUV', '798', '图瓦卢', '+688', '🇹🇻', TRUE, 2270, 0, 0),
  ('Taiwan', 'TW', 'TWN', '158', '中国台湾', '+886', '🇹🇼', TRUE, 2280, 0, 0),
  ('Tanzania', 'TZ', 'TZA', '834', '坦桑尼亚', '+255', '🇹🇿', TRUE, 2290, 0, 0),
  ('Ukraine', 'UA', 'UKR', '804', '乌克兰', '+380', '🇺🇦', TRUE, 2300, 0, 0),
  ('Uganda', 'UG', 'UGA', '800', '乌干达', '+256', '🇺🇬', TRUE, 2310, 0, 0),
  ('United States', 'US', 'USA', '840', '美国', '+1', '🇺🇸', TRUE, 2330, 0, 0),
  ('Uruguay', 'UY', 'URY', '858', '乌拉圭', '+598', '🇺🇾', TRUE, 2340, 0, 0),
  ('Uzbekistan', 'UZ', 'UZB', '860', '乌兹别克斯坦', '+998', '🇺🇿', TRUE, 2350, 0, 0),
  ('Vatican City', 'VA', 'VAT', '336', '梵蒂冈', '+39', '🇻🇦', TRUE, 2360, 0, 0),
  ('Venezuela', 'VE', 'VEN', '862', '委内瑞拉', '+58', '🇻🇪', TRUE, 2380, 0, 0),
  ('Vietnam', 'VN', 'VNM', '704', '越南', '+84', '🇻🇳', TRUE, 2410, 0, 0),
  ('Vanuatu', 'VU', 'VUT', '548', '瓦努阿图', '+678', '🇻🇺', TRUE, 2420, 0, 0),
  ('Samoa', 'WS', 'WSM', '882', '萨摩亚', '+685', '🇼🇸', TRUE, 2440, 0, 0),
  ('Kosovo', 'XK', 'UNK', '', '科索沃', '+383', '🇽🇰', TRUE, 2450, 0, 0),
  ('Yemen', 'YE', 'YEM', '887', '也门', '+967', '🇾🇪', TRUE, 2460, 0, 0),
  ('Mayotte', 'YT', 'MYT', '175', '马约特', '+262', '🇾🇹', TRUE, 2470, 0, 0),
  ('South Africa', 'ZA', 'ZAF', '710', '南非', '+27', '🇿🇦', TRUE, 2480, 0, 0),
  ('Zambia', 'ZM', 'ZMB', '894', '赞比亚', '+260', '🇿🇲', TRUE, 2490, 0, 0),
  ('Zimbabwe', 'ZW', 'ZWE', '716', '津巴布韦', '+263', '🇿🇼', TRUE, 2500, 0, 0)
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_at_ms = VALUES(updated_at_ms);

INSERT INTO regions (region_code, name, status, sort_order, created_at_ms, updated_at_ms) VALUES
  ('GLOBAL', 'GLOBAL', 'active', 0, 0, 0),
  ('Southern Europe', '南欧', 'active', 10, 0, 0),
  ('Western Asia', '西亚', 'active', 20, 0, 0),
  ('Southern Asia', '南亚', 'active', 30, 0, 0),
  ('Southeast Europe', '东南欧', 'active', 50, 0, 0),
  ('Middle Africa', '中非', 'active', 60, 0, 0),
  ('South America', '南美洲', 'active', 80, 0, 0),
  ('Central Europe', '中欧', 'active', 100, 0, 0),
  ('Northern Europe', '北欧', 'active', 120, 0, 0),
  ('Western Europe', '西欧', 'active', 130, 0, 0),
  ('Western Africa', '西非', 'active', 140, 0, 0),
  ('Eastern Africa', '东非', 'active', 150, 0, 0),
  ('North America', '北美洲', 'active', 160, 0, 0),
  ('South-Eastern Asia', '东南亚', 'active', 170, 0, 0),
  ('Eastern Europe', '东欧', 'active', 190, 0, 0),
  ('Central America', '中美洲', 'active', 200, 0, 0),
  ('Eastern Asia', '东亚', 'active', 210, 0, 0),
  ('Northern Africa', '北非', 'active', 220, 0, 0),
  ('Central Asia', '中亚', 'active', 250, 0, 0)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  status = VALUES(status),
  sort_order = VALUES(sort_order),
  updated_at_ms = VALUES(updated_at_ms);

INSERT INTO region_countries (app_code, region_id, country_code, status, created_at_ms, updated_at_ms)
SELECT 'lalu', r.region_id, seed.country_code, 'active', 0, 0
FROM (
  SELECT 'AD' AS country_code, 'Southern Europe' AS region_code
  UNION ALL
  SELECT 'AE' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'AF' AS country_code, 'Southern Asia' AS region_code
  UNION ALL
  SELECT 'AI' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'AL' AS country_code, 'Southeast Europe' AS region_code
  UNION ALL
  SELECT 'AM' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'AO' AS country_code, 'Middle Africa' AS region_code
  UNION ALL
  SELECT 'AR' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'AT' AS country_code, 'Central Europe' AS region_code
  UNION ALL
  SELECT 'AU' AS country_code, 'Australia and New Zealand' AS region_code
  UNION ALL
  SELECT 'AW' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'AX' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'AZ' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'BB' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'BD' AS country_code, 'Southern Asia' AS region_code
  UNION ALL
  SELECT 'BE' AS country_code, 'Western Europe' AS region_code
  UNION ALL
  SELECT 'BF' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'BG' AS country_code, 'Southeast Europe' AS region_code
  UNION ALL
  SELECT 'BH' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'BI' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'BJ' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'BL' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'BM' AS country_code, 'North America' AS region_code
  UNION ALL
  SELECT 'BN' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'BO' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'BR' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'BS' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'BT' AS country_code, 'Southern Asia' AS region_code
  UNION ALL
  SELECT 'BV' AS country_code, 'UNSPECIFIED' AS region_code
  UNION ALL
  SELECT 'BW' AS country_code, 'Southern Africa' AS region_code
  UNION ALL
  SELECT 'BY' AS country_code, 'Eastern Europe' AS region_code
  UNION ALL
  SELECT 'BZ' AS country_code, 'Central America' AS region_code
  UNION ALL
  SELECT 'CA' AS country_code, 'North America' AS region_code
  UNION ALL
  SELECT 'CC' AS country_code, 'Australia and New Zealand' AS region_code
  UNION ALL
  SELECT 'CD' AS country_code, 'Middle Africa' AS region_code
  UNION ALL
  SELECT 'CF' AS country_code, 'Middle Africa' AS region_code
  UNION ALL
  SELECT 'CG' AS country_code, 'Middle Africa' AS region_code
  UNION ALL
  SELECT 'CH' AS country_code, 'Western Europe' AS region_code
  UNION ALL
  SELECT 'CI' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'CL' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'CM' AS country_code, 'Middle Africa' AS region_code
  UNION ALL
  SELECT 'CN' AS country_code, 'Eastern Asia' AS region_code
  UNION ALL
  SELECT 'CO' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'CR' AS country_code, 'Central America' AS region_code
  UNION ALL
  SELECT 'CU' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'CV' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'CW' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'CX' AS country_code, 'Australia and New Zealand' AS region_code
  UNION ALL
  SELECT 'CY' AS country_code, 'Southern Europe' AS region_code
  UNION ALL
  SELECT 'CZ' AS country_code, 'Central Europe' AS region_code
  UNION ALL
  SELECT 'DE' AS country_code, 'Western Europe' AS region_code
  UNION ALL
  SELECT 'DJ' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'DK' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'DM' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'DO' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'DZ' AS country_code, 'Northern Africa' AS region_code
  UNION ALL
  SELECT 'EC' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'EE' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'EG' AS country_code, 'Northern Africa' AS region_code
  UNION ALL
  SELECT 'ER' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'ES' AS country_code, 'Southern Europe' AS region_code
  UNION ALL
  SELECT 'ET' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'FI' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'FJ' AS country_code, 'Melanesia' AS region_code
  UNION ALL
  SELECT 'FM' AS country_code, 'Micronesia' AS region_code
  UNION ALL
  SELECT 'FR' AS country_code, 'Western Europe' AS region_code
  UNION ALL
  SELECT 'GA' AS country_code, 'Middle Africa' AS region_code
  UNION ALL
  SELECT 'GB' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'GD' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'GE' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'GF' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'GG' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'GH' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'GI' AS country_code, 'Southern Europe' AS region_code
  UNION ALL
  SELECT 'GL' AS country_code, 'North America' AS region_code
  UNION ALL
  SELECT 'GM' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'GN' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'GQ' AS country_code, 'Middle Africa' AS region_code
  UNION ALL
  SELECT 'GR' AS country_code, 'Southern Europe' AS region_code
  UNION ALL
  SELECT 'GS' AS country_code, 'UNSPECIFIED' AS region_code
  UNION ALL
  SELECT 'GT' AS country_code, 'Central America' AS region_code
  UNION ALL
  SELECT 'GU' AS country_code, 'Micronesia' AS region_code
  UNION ALL
  SELECT 'GY' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'HN' AS country_code, 'Central America' AS region_code
  UNION ALL
  SELECT 'HR' AS country_code, 'Southeast Europe' AS region_code
  UNION ALL
  SELECT 'HT' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'HU' AS country_code, 'Central Europe' AS region_code
  UNION ALL
  SELECT 'ID' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'IE' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'IL' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'IM' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'IN' AS country_code, 'Southern Asia' AS region_code
  UNION ALL
  SELECT 'IQ' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'IR' AS country_code, 'Southern Asia' AS region_code
  UNION ALL
  SELECT 'IS' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'IT' AS country_code, 'Southern Europe' AS region_code
  UNION ALL
  SELECT 'JE' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'JM' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'JO' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'JP' AS country_code, 'Eastern Asia' AS region_code
  UNION ALL
  SELECT 'KE' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'KG' AS country_code, 'Central Asia' AS region_code
  UNION ALL
  SELECT 'KH' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'KI' AS country_code, 'Micronesia' AS region_code
  UNION ALL
  SELECT 'KM' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'KP' AS country_code, 'Eastern Asia' AS region_code
  UNION ALL
  SELECT 'KR' AS country_code, 'Eastern Asia' AS region_code
  UNION ALL
  SELECT 'KW' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'KY' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'KZ' AS country_code, 'Central Asia' AS region_code
  UNION ALL
  SELECT 'LA' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'LB' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'LC' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'LI' AS country_code, 'Western Europe' AS region_code
  UNION ALL
  SELECT 'LK' AS country_code, 'Southern Asia' AS region_code
  UNION ALL
  SELECT 'LR' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'LS' AS country_code, 'Southern Africa' AS region_code
  UNION ALL
  SELECT 'LT' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'LU' AS country_code, 'Western Europe' AS region_code
  UNION ALL
  SELECT 'LV' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'LY' AS country_code, 'Northern Africa' AS region_code
  UNION ALL
  SELECT 'MA' AS country_code, 'Northern Africa' AS region_code
  UNION ALL
  SELECT 'MC' AS country_code, 'Western Europe' AS region_code
  UNION ALL
  SELECT 'MD' AS country_code, 'Eastern Europe' AS region_code
  UNION ALL
  SELECT 'ME' AS country_code, 'Southeast Europe' AS region_code
  UNION ALL
  SELECT 'MF' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'MG' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'ML' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'MM' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'MN' AS country_code, 'Eastern Asia' AS region_code
  UNION ALL
  SELECT 'MO' AS country_code, 'Eastern Asia' AS region_code
  UNION ALL
  SELECT 'MQ' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'MR' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'MS' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'MT' AS country_code, 'Southern Europe' AS region_code
  UNION ALL
  SELECT 'MU' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'MV' AS country_code, 'Southern Asia' AS region_code
  UNION ALL
  SELECT 'MW' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'MX' AS country_code, 'North America' AS region_code
  UNION ALL
  SELECT 'MY' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'MZ' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'NA' AS country_code, 'Southern Africa' AS region_code
  UNION ALL
  SELECT 'NC' AS country_code, 'Melanesia' AS region_code
  UNION ALL
  SELECT 'NE' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'NF' AS country_code, 'Australia and New Zealand' AS region_code
  UNION ALL
  SELECT 'NG' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'NI' AS country_code, 'Central America' AS region_code
  UNION ALL
  SELECT 'NL' AS country_code, 'Western Europe' AS region_code
  UNION ALL
  SELECT 'NO' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'NP' AS country_code, 'Southern Asia' AS region_code
  UNION ALL
  SELECT 'NR' AS country_code, 'Micronesia' AS region_code
  UNION ALL
  SELECT 'NU' AS country_code, 'Polynesia' AS region_code
  UNION ALL
  SELECT 'NZ' AS country_code, 'Australia and New Zealand' AS region_code
  UNION ALL
  SELECT 'OM' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'PA' AS country_code, 'Central America' AS region_code
  UNION ALL
  SELECT 'PE' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'PH' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'PK' AS country_code, 'Southern Asia' AS region_code
  UNION ALL
  SELECT 'PL' AS country_code, 'Central Europe' AS region_code
  UNION ALL
  SELECT 'PR' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'PS' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'PT' AS country_code, 'Southern Europe' AS region_code
  UNION ALL
  SELECT 'PW' AS country_code, 'Micronesia' AS region_code
  UNION ALL
  SELECT 'PY' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'QA' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'RE' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'RO' AS country_code, 'Southeast Europe' AS region_code
  UNION ALL
  SELECT 'RS' AS country_code, 'Southeast Europe' AS region_code
  UNION ALL
  SELECT 'RU' AS country_code, 'Eastern Europe' AS region_code
  UNION ALL
  SELECT 'RW' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'SA' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'SB' AS country_code, 'Melanesia' AS region_code
  UNION ALL
  SELECT 'SC' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'SD' AS country_code, 'Northern Africa' AS region_code
  UNION ALL
  SELECT 'SE' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'SG' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'SI' AS country_code, 'Central Europe' AS region_code
  UNION ALL
  SELECT 'SJ' AS country_code, 'Northern Europe' AS region_code
  UNION ALL
  SELECT 'SK' AS country_code, 'Central Europe' AS region_code
  UNION ALL
  SELECT 'SL' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'SM' AS country_code, 'Southern Europe' AS region_code
  UNION ALL
  SELECT 'SN' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'SO' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'SR' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'SS' AS country_code, 'Middle Africa' AS region_code
  UNION ALL
  SELECT 'SV' AS country_code, 'Central America' AS region_code
  UNION ALL
  SELECT 'SX' AS country_code, 'Caribbean' AS region_code
  UNION ALL
  SELECT 'SY' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'SZ' AS country_code, 'Southern Africa' AS region_code
  UNION ALL
  SELECT 'TD' AS country_code, 'Middle Africa' AS region_code
  UNION ALL
  SELECT 'TG' AS country_code, 'Western Africa' AS region_code
  UNION ALL
  SELECT 'TH' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'TJ' AS country_code, 'Central Asia' AS region_code
  UNION ALL
  SELECT 'TK' AS country_code, 'Polynesia' AS region_code
  UNION ALL
  SELECT 'TL' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'TM' AS country_code, 'Central Asia' AS region_code
  UNION ALL
  SELECT 'TN' AS country_code, 'Northern Africa' AS region_code
  UNION ALL
  SELECT 'TO' AS country_code, 'Polynesia' AS region_code
  UNION ALL
  SELECT 'TR' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'TV' AS country_code, 'Polynesia' AS region_code
  UNION ALL
  SELECT 'TW' AS country_code, 'Eastern Asia' AS region_code
  UNION ALL
  SELECT 'TZ' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'UA' AS country_code, 'Eastern Europe' AS region_code
  UNION ALL
  SELECT 'UG' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'US' AS country_code, 'North America' AS region_code
  UNION ALL
  SELECT 'UY' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'UZ' AS country_code, 'Central Asia' AS region_code
  UNION ALL
  SELECT 'VA' AS country_code, 'Southern Europe' AS region_code
  UNION ALL
  SELECT 'VE' AS country_code, 'South America' AS region_code
  UNION ALL
  SELECT 'VN' AS country_code, 'South-Eastern Asia' AS region_code
  UNION ALL
  SELECT 'VU' AS country_code, 'Melanesia' AS region_code
  UNION ALL
  SELECT 'WS' AS country_code, 'Polynesia' AS region_code
  UNION ALL
  SELECT 'XK' AS country_code, 'Southeast Europe' AS region_code
  UNION ALL
  SELECT 'YE' AS country_code, 'Western Asia' AS region_code
  UNION ALL
  SELECT 'YT' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'ZA' AS country_code, 'Southern Africa' AS region_code
  UNION ALL
  SELECT 'ZM' AS country_code, 'Eastern Africa' AS region_code
  UNION ALL
  SELECT 'ZW' AS country_code, 'Eastern Africa' AS region_code
) AS seed
INNER JOIN regions r ON r.app_code = 'lalu' AND r.region_code = seed.region_code
  AND r.region_code NOT IN ('Australia and New Zealand', 'Caribbean', 'Melanesia', 'Micronesia', 'Polynesia', 'Southern Africa', 'UNSPECIFIED')
INNER JOIN countries c ON c.app_code = 'lalu' AND c.country_code = seed.country_code
ON DUPLICATE KEY UPDATE
  region_id = VALUES(region_id),
  status = VALUES(status),
  updated_at_ms = VALUES(updated_at_ms);

-- Product-excluded countries are hard removed so reapplying initdb updates existing local databases as well as fresh volumes.
DELETE FROM region_countries
WHERE app_code = 'lalu'
  AND country_code IN ('AG', 'AQ', 'AS', 'BA', 'BQ', 'CK', 'EH', 'FK', 'FO', 'GP', 'GW', 'HK', 'HM', 'IO', 'KN', 'MH', 'MK', 'MP', 'PF', 'PG', 'PM', 'PN', 'SH', 'ST', 'TC', 'TF', 'TT', 'UM', 'VC', 'VG', 'VI', 'WF');

DELETE FROM countries
WHERE app_code = 'lalu'
  AND country_code IN ('AG', 'AQ', 'AS', 'BA', 'BQ', 'CK', 'EH', 'FK', 'FO', 'GP', 'GW', 'HK', 'HM', 'IO', 'KN', 'MH', 'MK', 'MP', 'PF', 'PG', 'PM', 'PN', 'SH', 'ST', 'TC', 'TF', 'TT', 'UM', 'VC', 'VG', 'VI', 'WF');

CREATE TABLE IF NOT EXISTS user_display_user_ids (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  display_user_id VARCHAR(32) NOT NULL,
  user_id BIGINT NOT NULL,
  display_user_id_kind VARCHAR(32) NOT NULL,
  status VARCHAR(32) NOT NULL,
  assigned_at_ms BIGINT NOT NULL,
  activated_at_ms BIGINT NULL,
  expires_at_ms BIGINT NULL,
  released_at_ms BIGINT NULL,
  release_reason VARCHAR(64) NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  live_display_user_id VARCHAR(32)
    GENERATED ALWAYS AS (
      CASE
        WHEN status IN ('active', 'held', 'reserved', 'blocked') THEN display_user_id
        ELSE NULL
      END
    ) STORED,
  active_user_id BIGINT
    GENERATED ALWAYS AS (
      CASE WHEN status = 'active' THEN user_id ELSE NULL END
    ) STORED,
  UNIQUE KEY uk_user_display_user_ids_live (app_code, live_display_user_id),
  UNIQUE KEY uk_user_display_user_ids_active_user (app_code, active_user_id),
  KEY idx_user_display_user_ids_user_id (app_code, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS pretty_display_user_id_leases (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  lease_id VARCHAR(64) NOT NULL PRIMARY KEY,
  display_user_id VARCHAR(32) NOT NULL,
  user_id BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  starts_at_ms BIGINT NOT NULL,
  expires_at_ms BIGINT NOT NULL,
  payment_receipt_id VARCHAR(64) NULL,
  source VARCHAR(32) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  active_display_user_id VARCHAR(32)
    GENERATED ALWAYS AS (
      CASE WHEN status = 'active' THEN display_user_id ELSE NULL END
    ) STORED,
  active_user_id BIGINT
    GENERATED ALWAYS AS (
      CASE WHEN status = 'active' THEN user_id ELSE NULL END
    ) STORED,
  UNIQUE KEY uk_pretty_display_user_id_active (app_code, active_display_user_id),
  UNIQUE KEY uk_pretty_display_user_id_active_user (app_code, active_user_id),
  KEY idx_pretty_display_user_id_leases_user (app_code, user_id, status),
  KEY idx_pretty_display_user_id_leases_expire (app_code, status, expires_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS display_user_id_change_logs (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id BIGINT NOT NULL,
  old_display_user_id VARCHAR(32) NOT NULL,
  new_display_user_id VARCHAR(32) NOT NULL,
  old_display_user_id_kind VARCHAR(32) NOT NULL,
  new_display_user_id_kind VARCHAR(32) NOT NULL,
  change_type VARCHAR(32) NOT NULL,
  lease_id VARCHAR(64) NULL,
  reason VARCHAR(64) NOT NULL,
  operator_user_id BIGINT NULL,
  request_id VARCHAR(96) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  KEY idx_display_user_id_change_logs_user_id (app_code, user_id),
  KEY idx_display_user_id_change_logs_request_id (app_code, request_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_country_change_logs (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id BIGINT NOT NULL,
  old_country VARCHAR(64) NULL,
  new_country VARCHAR(64) NOT NULL,
  request_id VARCHAR(96) NOT NULL,
  changed_at_ms BIGINT NOT NULL,
  KEY idx_user_country_change_logs_user_changed (app_code, user_id, changed_at_ms),
  KEY idx_user_country_change_logs_request_id (app_code, request_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS password_accounts (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  password_hash VARCHAR(255) NOT NULL,
  hash_alg VARCHAR(32) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS third_party_identities (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id BIGINT NOT NULL,
  provider VARCHAR(64) NOT NULL,
  provider_subject VARCHAR(191) NOT NULL,
  provider_union_id VARCHAR(191) NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_third_party_provider_subject (app_code, provider, provider_subject),
  KEY idx_third_party_user_id (app_code, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS auth_sessions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  session_id VARCHAR(96) NOT NULL PRIMARY KEY,
  user_id BIGINT NOT NULL,
  refresh_token_hash VARCHAR(128) NOT NULL,
  device_id VARCHAR(128) NOT NULL,
  expires_at_ms BIGINT NOT NULL,
  revoked_at_ms BIGINT NULL,
  revoked_reason VARCHAR(64) NOT NULL DEFAULT '',
  revoked_request_id VARCHAR(96) NOT NULL DEFAULT '',
  revoked_by VARCHAR(64) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_auth_sessions_refresh_token_hash (app_code, refresh_token_hash),
  KEY idx_auth_sessions_user_id (app_code, user_id),
  KEY idx_auth_sessions_expires_at (app_code, expires_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS login_audit (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  request_id VARCHAR(96) NOT NULL,
  user_id BIGINT NULL,
  login_type VARCHAR(32) NOT NULL,
  provider VARCHAR(64) NULL,
  channel VARCHAR(32) NOT NULL DEFAULT '',
  platform VARCHAR(16) NOT NULL DEFAULT '',
  result VARCHAR(32) NOT NULL,
  failure_code VARCHAR(64) NULL,
  client_ip VARCHAR(64) NULL,
  ip_country_code VARCHAR(3) NOT NULL DEFAULT '',
  blocked TINYINT(1) NOT NULL DEFAULT 0,
  block_reason VARCHAR(64) NOT NULL DEFAULT '',
  user_agent VARCHAR(512) NULL,
  created_at_ms BIGINT NOT NULL,
  KEY idx_login_audit_user_id (app_code, user_id),
  KEY idx_login_audit_request_id (app_code, request_id),
  KEY idx_login_audit_created_at (app_code, created_at_ms),
  KEY idx_login_audit_user_created (app_code, user_id, created_at_ms),
  KEY idx_login_audit_blocked (app_code, blocked, created_at_ms),
  KEY idx_login_audit_ip_country (app_code, ip_country_code, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS login_ip_risk_jobs (
  app_code VARCHAR(32) NOT NULL,
  job_id VARCHAR(96) NOT NULL,
  session_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  client_ip VARCHAR(64) NOT NULL,
  client_ip_hash VARCHAR(128) NOT NULL,
  channel VARCHAR(32) NOT NULL,
  platform VARCHAR(16) NOT NULL,
  language VARCHAR(32) NOT NULL DEFAULT '',
  timezone VARCHAR(64) NOT NULL DEFAULT '',
  request_id VARCHAR(96) NOT NULL DEFAULT '',
  edge_country_code VARCHAR(3) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL,
  decision VARCHAR(32) NOT NULL DEFAULT '',
  country_code VARCHAR(3) NOT NULL DEFAULT '',
  failure_reason VARCHAR(128) NOT NULL DEFAULT '',
  locked_by VARCHAR(128) NULL,
  locked_until_ms BIGINT NULL,
  attempt_count INT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, job_id),
  KEY idx_login_ip_risk_jobs_status (app_code, status, updated_at_ms),
  KEY idx_login_ip_risk_jobs_session (app_code, session_id),
  KEY idx_login_ip_risk_jobs_user (app_code, user_id, created_at_ms),
  KEY idx_login_ip_risk_jobs_ip (app_code, client_ip_hash, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS login_ip_risk_decisions (
  app_code VARCHAR(32) NOT NULL,
  decision_id VARCHAR(96) NOT NULL,
  client_ip VARCHAR(64) NOT NULL,
  client_ip_hash VARCHAR(128) NOT NULL,
  decision VARCHAR(32) NOT NULL,
  country_code VARCHAR(3) NOT NULL DEFAULT '',
  confidence INT NOT NULL DEFAULT 0,
  provider_attempts_json JSON NOT NULL,
  decided_at_ms BIGINT NOT NULL,
  expires_at_ms BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, decision_id),
  KEY idx_login_ip_risk_decisions_ip (app_code, client_ip_hash, decided_at_ms),
  KEY idx_login_ip_risk_decisions_country (app_code, country_code, decided_at_ms),
  KEY idx_login_ip_risk_decisions_expires (app_code, expires_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 本地联调测试账号。
-- 密码登录仍以 users.current_display_user_id 为账号，password_accounts 只保存 user_id 维度密码身份。
INSERT INTO users (
  app_code,
  user_id,
  default_display_user_id,
  current_display_user_id,
  current_display_user_id_kind,
  current_display_user_id_expires_at_ms,
  username,
  profile_completed,
  profile_completed_at_ms,
  onboarding_status,
  status,
  created_at_ms,
  updated_at_ms
)
VALUES (
  'lalu',
  123456,
  '123456',
  '123456',
  'default',
  NULL,
  'test_123456',
  1,
  0,
  'completed',
  'active',
  0,
  0
)
ON DUPLICATE KEY UPDATE
  current_display_user_id = VALUES(current_display_user_id),
  current_display_user_id_kind = VALUES(current_display_user_id_kind),
  current_display_user_id_expires_at_ms = VALUES(current_display_user_id_expires_at_ms),
  username = VALUES(username),
  profile_completed = VALUES(profile_completed),
  onboarding_status = VALUES(onboarding_status),
  status = VALUES(status),
  updated_at_ms = VALUES(updated_at_ms);

INSERT INTO user_display_user_ids (
  app_code,
  display_user_id,
  user_id,
  display_user_id_kind,
  status,
  assigned_at_ms,
  activated_at_ms,
  created_at_ms,
  updated_at_ms
)
VALUES ('lalu', '123456', 123456, 'default', 'active', 0, 0, 0, 0)
ON DUPLICATE KEY UPDATE
  display_user_id = VALUES(display_user_id),
  display_user_id_kind = VALUES(display_user_id_kind),
  status = VALUES(status),
  activated_at_ms = VALUES(activated_at_ms),
  updated_at_ms = VALUES(updated_at_ms);

INSERT INTO password_accounts (
  app_code,
  user_id,
  password_hash,
  hash_alg,
  created_at_ms,
  updated_at_ms
)
VALUES (
  'lalu',
  123456,
  '$2a$10$9eyyJvfGdkb3oU8EAPUHiuLz1C/AmhGTU1nq8AeOIMNFwznk7B69G',
  'bcrypt',
  0,
  0
)
ON DUPLICATE KEY UPDATE
  password_hash = VALUES(password_hash),
  hash_alg = VALUES(hash_alg),
  updated_at_ms = VALUES(updated_at_ms);

INSERT INTO users (
  app_code,
  user_id,
  default_display_user_id,
  current_display_user_id,
  current_display_user_id_kind,
  current_display_user_id_expires_at_ms,
  username,
  profile_completed,
  profile_completed_at_ms,
  onboarding_status,
  status,
  created_at_ms,
  updated_at_ms
)
VALUES (
  'lalu',
  12345678,
  '12345678',
  '12345678',
  'default',
  NULL,
  'test_12345678',
  1,
  0,
  'completed',
  'active',
  0,
  0
)
ON DUPLICATE KEY UPDATE
  current_display_user_id = VALUES(current_display_user_id),
  current_display_user_id_kind = VALUES(current_display_user_id_kind),
  current_display_user_id_expires_at_ms = VALUES(current_display_user_id_expires_at_ms),
  username = VALUES(username),
  profile_completed = VALUES(profile_completed),
  onboarding_status = VALUES(onboarding_status),
  status = VALUES(status),
  updated_at_ms = VALUES(updated_at_ms);

INSERT INTO user_display_user_ids (
  app_code,
  display_user_id,
  user_id,
  display_user_id_kind,
  status,
  assigned_at_ms,
  activated_at_ms,
  created_at_ms,
  updated_at_ms
)
VALUES ('lalu', '12345678', 12345678, 'default', 'active', 0, 0, 0, 0)
ON DUPLICATE KEY UPDATE
  display_user_id = VALUES(display_user_id),
  display_user_id_kind = VALUES(display_user_id_kind),
  status = VALUES(status),
  activated_at_ms = VALUES(activated_at_ms),
  updated_at_ms = VALUES(updated_at_ms);

INSERT INTO password_accounts (
  app_code,
  user_id,
  password_hash,
  hash_alg,
  created_at_ms,
  updated_at_ms
)
VALUES (
  'lalu',
  12345678,
  '$2a$10$2sdtdAzFlPu23vC0vMQT6OIIm5.KCzATuwvn8i09/SKHz.hmkzJce',
  'bcrypt',
  0,
  0
)
ON DUPLICATE KEY UPDATE
  password_hash = VALUES(password_hash),
  hash_alg = VALUES(hash_alg),
  updated_at_ms = VALUES(updated_at_ms);
