SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

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

USE hyapp_activity;

CREATE TABLE IF NOT EXISTS activities (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  activity_id VARCHAR(96) NOT NULL,
  status VARCHAR(32) NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, activity_id),
  KEY idx_activities_status (app_code, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS activity_event_consumption (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  event_id VARCHAR(96) NOT NULL,
  event_type VARCHAR(96) NOT NULL,
  source VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  failure_reason VARCHAR(255) NULL,
  consumed_at_ms BIGINT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, event_id),
  KEY idx_activity_event_consumption_status (app_code, status),
  KEY idx_activity_event_consumption_updated_at (app_code, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS activity_outbox (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  outbox_id VARCHAR(96) NOT NULL,
  event_type VARCHAR(96) NOT NULL,
  payload BLOB NOT NULL,
  status VARCHAR(32) NOT NULL,
  retry_count INT NOT NULL DEFAULT 0,
  next_retry_at_ms BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, outbox_id),
  KEY idx_activity_outbox_status_retry (app_code, status, next_retry_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS im_broadcast_outbox (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  event_id VARCHAR(128) NOT NULL,
  scope VARCHAR(32) NOT NULL,
  group_id VARCHAR(64) NOT NULL,
  broadcast_type VARCHAR(64) NOT NULL,
  payload_json JSON NOT NULL,
  status VARCHAR(32) NOT NULL,
  attempt_count INT NOT NULL DEFAULT 0,
  next_retry_at_ms BIGINT NOT NULL DEFAULT 0,
  last_error VARCHAR(512) NOT NULL DEFAULT '',
  locked_by VARCHAR(128) NOT NULL DEFAULT '',
  locked_until_ms BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, event_id),
  KEY idx_im_broadcast_outbox_pending (app_code, status, next_retry_at_ms, created_at_ms),
  KEY idx_im_broadcast_outbox_lock (app_code, status, locked_until_ms, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS task_definitions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  task_id VARCHAR(96) NOT NULL,
  task_type VARCHAR(32) NOT NULL,
  category VARCHAR(32) NOT NULL,
  metric_type VARCHAR(64) NOT NULL,
  title VARCHAR(160) NOT NULL,
  description VARCHAR(512) NOT NULL DEFAULT '',
  target_value BIGINT NOT NULL,
  target_unit VARCHAR(32) NOT NULL,
  reward_coin_amount BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  version BIGINT NOT NULL,
  current_task_version_id BIGINT NOT NULL DEFAULT 0,
  effective_from_ms BIGINT NOT NULL DEFAULT 0,
  effective_to_ms BIGINT NOT NULL DEFAULT 0,
  created_by_admin_id BIGINT NOT NULL DEFAULT 0,
  updated_by_admin_id BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, task_id),
  KEY idx_task_definitions_visible (app_code, task_type, status, sort_order),
  KEY idx_task_definitions_metric (app_code, metric_type, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS task_definition_versions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  task_version_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  task_id VARCHAR(96) NOT NULL,
  version BIGINT NOT NULL,
  snapshot_json JSON NOT NULL,
  effective_from_cycle VARCHAR(32) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_task_definition_versions_version (app_code, task_id, version),
  KEY idx_task_definition_versions_task (app_code, task_id, task_version_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_task_progress (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  task_id VARCHAR(96) NOT NULL,
  task_version_id BIGINT NOT NULL,
  cycle_key VARCHAR(32) NOT NULL,
  progress_value BIGINT NOT NULL,
  target_value BIGINT NOT NULL,
  reward_coin_amount BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  completed_at_ms BIGINT NOT NULL DEFAULT 0,
  claimed_at_ms BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id, task_id, cycle_key),
  KEY idx_user_task_progress_user_cycle (app_code, user_id, cycle_key, status),
  KEY idx_user_task_progress_task_cycle (app_code, task_id, cycle_key, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS task_event_consumption (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  event_id VARCHAR(128) NOT NULL,
  event_type VARCHAR(96) NOT NULL,
  source_service VARCHAR(64) NOT NULL,
  user_id BIGINT NOT NULL,
  task_day VARCHAR(32) NOT NULL,
  status VARCHAR(32) NOT NULL,
  skip_reason VARCHAR(255) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  consumed_at_ms BIGINT NOT NULL DEFAULT 0,
  PRIMARY KEY (app_code, event_id),
  KEY idx_task_event_consumption_status (app_code, status, consumed_at_ms),
  KEY idx_task_event_consumption_user_day (app_code, user_id, task_day)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS task_reward_claims (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  claim_id VARCHAR(96) NOT NULL,
  command_id VARCHAR(128) NOT NULL,
  user_id BIGINT NOT NULL,
  task_id VARCHAR(96) NOT NULL,
  task_type VARCHAR(32) NOT NULL,
  cycle_key VARCHAR(32) NOT NULL,
  reward_coin_amount BIGINT NOT NULL,
  wallet_command_id VARCHAR(128) NOT NULL,
  wallet_transaction_id VARCHAR(96) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL,
  failure_reason VARCHAR(255) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, claim_id),
  UNIQUE KEY uk_task_reward_claim_command (app_code, command_id),
  UNIQUE KEY uk_task_reward_claim_once (app_code, user_id, task_id, cycle_key),
  KEY idx_task_reward_claim_status (app_code, status, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS registration_reward_configs (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  enabled TINYINT(1) NOT NULL DEFAULT 0,
  reward_type VARCHAR(32) NOT NULL DEFAULT 'coin',
  coin_amount BIGINT NOT NULL DEFAULT 0,
  resource_group_id BIGINT NOT NULL DEFAULT 0,
  daily_limit BIGINT NOT NULL DEFAULT 0,
  updated_by_admin_id BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS registration_reward_claims (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  claim_id VARCHAR(96) NOT NULL,
  command_id VARCHAR(128) NOT NULL,
  user_id BIGINT NOT NULL,
  reward_day VARCHAR(32) NOT NULL,
  reward_type VARCHAR(32) NOT NULL,
  coin_amount BIGINT NOT NULL DEFAULT 0,
  resource_group_id BIGINT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL,
  wallet_command_id VARCHAR(128) NOT NULL,
  wallet_transaction_id VARCHAR(96) NOT NULL DEFAULT '',
  resource_grant_id VARCHAR(96) NOT NULL DEFAULT '',
  failure_reason VARCHAR(255) NOT NULL DEFAULT '',
  claimed_at_ms BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, claim_id),
  UNIQUE KEY uk_registration_reward_command (app_code, command_id),
  UNIQUE KEY uk_registration_reward_user_once (app_code, user_id),
  KEY idx_registration_reward_list (app_code, created_at_ms, claim_id),
  KEY idx_registration_reward_status (app_code, status, updated_at_ms),
  KEY idx_registration_reward_day (app_code, reward_day, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS registration_reward_daily_counters (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  reward_day VARCHAR(32) NOT NULL,
  reserved_count BIGINT NOT NULL DEFAULT 0,
  granted_count BIGINT NOT NULL DEFAULT 0,
  failed_count BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, reward_day)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS growth_level_tracks (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  track VARCHAR(32) NOT NULL,
  name VARCHAR(96) NOT NULL,
  status VARCHAR(32) NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  display_config_json JSON NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, track),
  KEY idx_growth_level_tracks_status_sort (app_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS growth_level_rules (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  track VARCHAR(32) NOT NULL,
  level INT NOT NULL,
  required_value BIGINT NOT NULL,
  name VARCHAR(96) NOT NULL,
  status VARCHAR(32) NOT NULL,
  reward_resource_group_id BIGINT NOT NULL DEFAULT 0,
  sort_order INT NOT NULL DEFAULT 0,
  display_config_json JSON NULL,
  created_by_admin_id BIGINT NOT NULL DEFAULT 0,
  updated_by_admin_id BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, track, level),
  KEY idx_growth_level_rules_active (app_code, track, status, level),
  KEY idx_growth_level_rules_required (app_code, track, status, required_value)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS growth_level_tiers (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  tier_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  track VARCHAR(32) NOT NULL,
  min_level INT NOT NULL,
  max_level INT NOT NULL,
  name VARCHAR(96) NOT NULL,
  display_avatar_frame_resource_id BIGINT NOT NULL DEFAULT 0,
  display_badge_resource_id BIGINT NOT NULL DEFAULT 0,
  reward_resource_group_id BIGINT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL,
  display_config_json JSON NULL,
  created_by_admin_id BIGINT NOT NULL DEFAULT 0,
  updated_by_admin_id BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_growth_level_tiers_track_range (app_code, track, min_level, max_level),
  KEY idx_growth_level_tiers_active (app_code, track, status, min_level, max_level)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_growth_level_accounts (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  track VARCHAR(32) NOT NULL,
  total_value BIGINT NOT NULL DEFAULT 0,
  current_level INT NOT NULL DEFAULT 0,
  current_tier_id BIGINT NOT NULL DEFAULT 0,
  level_updated_at_ms BIGINT NOT NULL DEFAULT 0,
  tier_updated_at_ms BIGINT NOT NULL DEFAULT 0,
  version BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id, track),
  KEY idx_user_growth_level_accounts_track_level (app_code, track, current_level, total_value)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS growth_level_value_events (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  value_event_id VARCHAR(160) NOT NULL,
  source_event_id VARCHAR(128) NOT NULL,
  source_service VARCHAR(64) NOT NULL,
  source_event_type VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  track VARCHAR(32) NOT NULL,
  metric_type VARCHAR(64) NOT NULL,
  value_delta BIGINT NOT NULL,
  occurred_at_ms BIGINT NOT NULL,
  dimensions_json JSON NULL,
  created_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, value_event_id),
  KEY idx_growth_value_user_track (app_code, user_id, track, occurred_at_ms),
  KEY idx_growth_value_source (app_code, source_service, source_event_type, source_event_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_growth_level_history (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  history_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  track VARCHAR(32) NOT NULL,
  previous_level INT NOT NULL,
  new_level INT NOT NULL,
  previous_tier_id BIGINT NOT NULL DEFAULT 0,
  new_tier_id BIGINT NOT NULL DEFAULT 0,
  total_value BIGINT NOT NULL,
  value_event_id VARCHAR(160) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, history_id),
  KEY idx_growth_history_user_track (app_code, user_id, track, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS growth_level_reward_jobs (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  reward_job_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  track VARCHAR(32) NOT NULL,
  reward_source_type VARCHAR(32) NOT NULL,
  reward_source_id VARCHAR(96) NOT NULL,
  resource_group_id BIGINT NOT NULL,
  wallet_command_id VARCHAR(128) NOT NULL,
  wallet_grant_id VARCHAR(96) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL,
  attempt_count INT NOT NULL DEFAULT 0,
  next_retry_at_ms BIGINT NOT NULL DEFAULT 0,
  locked_by VARCHAR(128) NOT NULL DEFAULT '',
  locked_until_ms BIGINT NOT NULL DEFAULT 0,
  failure_reason VARCHAR(255) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, reward_job_id),
  UNIQUE KEY uk_growth_level_reward_once (app_code, user_id, track, reward_source_type, reward_source_id),
  KEY idx_growth_level_reward_pending (app_code, status, next_retry_at_ms, created_at_ms),
  KEY idx_growth_level_reward_user (app_code, user_id, track, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_level_display_profiles (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  wealth_level INT NOT NULL DEFAULT 0,
  wealth_tier_id BIGINT NOT NULL DEFAULT 0,
  wealth_avatar_frame_resource_id BIGINT NOT NULL DEFAULT 0,
  wealth_badge_resource_id BIGINT NOT NULL DEFAULT 0,
  game_level INT NOT NULL DEFAULT 0,
  game_tier_id BIGINT NOT NULL DEFAULT 0,
  game_avatar_frame_resource_id BIGINT NOT NULL DEFAULT 0,
  game_badge_resource_id BIGINT NOT NULL DEFAULT 0,
  charm_level INT NOT NULL DEFAULT 0,
  charm_tier_id BIGINT NOT NULL DEFAULT 0,
  charm_avatar_frame_resource_id BIGINT NOT NULL DEFAULT 0,
  charm_badge_resource_id 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;

INSERT INTO growth_level_tracks (app_code, track, name, status, sort_order, display_config_json, created_at_ms, updated_at_ms)
VALUES
  ('lalu', 'wealth', '财富等级', 'active', 10, JSON_OBJECT(), 0, 0),
  ('lalu', 'game', '游戏等级', 'active', 20, JSON_OBJECT(), 0, 0),
  ('lalu', 'charm', '魅力等级', 'active', 30, JSON_OBJECT(), 0, 0)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  status = VALUES(status),
  sort_order = VALUES(sort_order),
  updated_at_ms = VALUES(updated_at_ms);

CREATE TABLE IF NOT EXISTS achievement_definitions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  achievement_id VARCHAR(96) NOT NULL,
  collection_id VARCHAR(96) NOT NULL DEFAULT '',
  collection_type VARCHAR(32) NOT NULL,
  achievement_type VARCHAR(32) NOT NULL,
  title VARCHAR(160) NOT NULL,
  description VARCHAR(512) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL,
  version BIGINT NOT NULL DEFAULT 1,
  primary_badge_resource_id BIGINT NOT NULL DEFAULT 0,
  reward_resource_group_id BIGINT NOT NULL DEFAULT 0,
  auto_pin_policy VARCHAR(32) NOT NULL DEFAULT 'none',
  honor_visibility TINYINT(1) NOT NULL DEFAULT 1,
  effective_from_ms BIGINT NOT NULL DEFAULT 0,
  effective_to_ms BIGINT NOT NULL DEFAULT 0,
  sort_order INT NOT NULL DEFAULT 0,
  display_config_json JSON NULL,
  created_by_admin_id BIGINT NOT NULL DEFAULT 0,
  updated_by_admin_id BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, achievement_id),
  KEY idx_achievement_definitions_visible (app_code, collection_type, status, sort_order),
  KEY idx_achievement_definitions_effective (app_code, status, effective_from_ms, effective_to_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS achievement_conditions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  condition_id VARCHAR(96) NOT NULL,
  achievement_id VARCHAR(96) NOT NULL,
  version BIGINT NOT NULL,
  metric_type VARCHAR(64) NOT NULL,
  target_value BIGINT NOT NULL,
  target_unit VARCHAR(32) NOT NULL,
  dimension_filter_json JSON NULL,
  created_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, condition_id),
  KEY idx_achievement_conditions_achievement (app_code, achievement_id, version),
  KEY idx_achievement_conditions_metric (app_code, metric_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_achievement_progress (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  achievement_id VARCHAR(96) NOT NULL,
  condition_id VARCHAR(96) NOT NULL,
  cycle_key VARCHAR(96) NOT NULL,
  progress_value BIGINT NOT NULL DEFAULT 0,
  target_value BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id, achievement_id, condition_id, cycle_key),
  KEY idx_user_achievement_progress_user (app_code, user_id, cycle_key, status),
  KEY idx_user_achievement_progress_achievement (app_code, achievement_id, cycle_key, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_achievement_unlocks (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  achievement_id VARCHAR(96) NOT NULL,
  cycle_key VARCHAR(96) NOT NULL,
  version BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  unlocked_at_ms BIGINT NOT NULL,
  reward_job_id VARCHAR(96) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id, achievement_id, cycle_key),
  KEY idx_user_achievement_unlocks_user (app_code, user_id, unlocked_at_ms),
  KEY idx_user_achievement_unlocks_reward (app_code, reward_job_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS achievement_event_consumption (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  event_id VARCHAR(128) NOT NULL,
  event_type VARCHAR(96) NOT NULL,
  source_service VARCHAR(64) NOT NULL,
  user_id BIGINT NOT NULL,
  metric_type VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  skip_reason VARCHAR(255) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  consumed_at_ms BIGINT NOT NULL DEFAULT 0,
  PRIMARY KEY (app_code, event_id),
  KEY idx_achievement_event_status (app_code, status, consumed_at_ms),
  KEY idx_achievement_event_user (app_code, user_id, metric_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS achievement_reward_jobs (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  reward_job_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  achievement_id VARCHAR(96) NOT NULL,
  cycle_key VARCHAR(96) NOT NULL,
  resource_group_id BIGINT NOT NULL,
  wallet_command_id VARCHAR(128) NOT NULL,
  wallet_grant_id VARCHAR(96) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL,
  attempt_count INT NOT NULL DEFAULT 0,
  next_retry_at_ms BIGINT NOT NULL DEFAULT 0,
  locked_by VARCHAR(128) NOT NULL DEFAULT '',
  locked_until_ms BIGINT NOT NULL DEFAULT 0,
  failure_reason VARCHAR(255) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, reward_job_id),
  UNIQUE KEY uk_achievement_reward_once (app_code, user_id, achievement_id, cycle_key),
  KEY idx_achievement_reward_pending (app_code, status, next_retry_at_ms, created_at_ms),
  KEY idx_achievement_reward_user (app_code, user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_badge_display_slots (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  slot VARCHAR(32) NOT NULL,
  position INT NOT NULL,
  badge_form VARCHAR(32) NOT NULL,
  resource_id BIGINT NOT NULL,
  entitlement_id VARCHAR(96) NOT NULL DEFAULT '',
  source_type VARCHAR(32) NOT NULL,
  source_id VARCHAR(128) NOT NULL DEFAULT '',
  pin_mode VARCHAR(32) NOT NULL DEFAULT '',
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id, slot, position),
  UNIQUE KEY uk_user_badge_display_resource (app_code, user_id, slot, resource_id),
  KEY idx_user_badge_display_user (app_code, user_id, slot, updated_at_ms),
  KEY idx_user_badge_display_resource (app_code, resource_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS message_templates (
  app_code VARCHAR(32) NOT NULL,
  template_id VARCHAR(96) NOT NULL,
  template_version VARCHAR(64) NOT NULL,
  message_type VARCHAR(32) NOT NULL,
  locale VARCHAR(32) NOT NULL DEFAULT '',
  title VARCHAR(160) NOT NULL,
  summary VARCHAR(512) NOT NULL,
  body VARCHAR(2048) NOT NULL DEFAULT '',
  icon_url VARCHAR(512) NOT NULL DEFAULT '',
  image_url VARCHAR(512) NOT NULL DEFAULT '',
  action_type VARCHAR(64) NOT NULL DEFAULT '',
  action_param VARCHAR(1024) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL,
  created_by VARCHAR(64) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, template_id, template_version),
  KEY idx_message_templates_type_status (app_code, message_type, status),
  KEY idx_message_templates_template_status (app_code, template_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_inbox_messages (
  app_code VARCHAR(32) NOT NULL,
  inbox_message_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  message_type VARCHAR(32) NOT NULL,
  producer VARCHAR(64) NOT NULL,
  producer_event_id VARCHAR(128) NOT NULL,
  producer_event_type VARCHAR(96) NOT NULL DEFAULT '',
  aggregate_type VARCHAR(64) NOT NULL DEFAULT '',
  aggregate_id VARCHAR(128) NOT NULL DEFAULT '',
  template_id VARCHAR(96) NOT NULL DEFAULT '',
  template_version VARCHAR(64) NOT NULL DEFAULT '',
  title VARCHAR(160) NOT NULL,
  summary VARCHAR(512) NOT NULL,
  body VARCHAR(2048) NOT NULL DEFAULT '',
  icon_url VARCHAR(512) NOT NULL DEFAULT '',
  image_url VARCHAR(512) NOT NULL DEFAULT '',
  action_type VARCHAR(64) NOT NULL DEFAULT '',
  action_param VARCHAR(1024) NOT NULL DEFAULT '',
  priority INT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL,
  read_at_ms BIGINT NULL,
  deleted_at_ms BIGINT NULL,
  sent_at_ms BIGINT NOT NULL,
  expire_at_ms BIGINT NULL,
  metadata_json JSON NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, inbox_message_id),
  UNIQUE KEY uk_user_inbox_producer_event (app_code, user_id, producer, producer_event_id),
  KEY idx_user_inbox_list (app_code, user_id, message_type, deleted_at_ms, status, sent_at_ms, inbox_message_id),
  KEY idx_user_inbox_unread (app_code, user_id, message_type, read_at_ms, deleted_at_ms, status, expire_at_ms),
  KEY idx_user_inbox_aggregate (app_code, user_id, aggregate_type, aggregate_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS message_fanout_jobs (
  app_code VARCHAR(32) NOT NULL,
  job_id VARCHAR(96) NOT NULL,
  command_id VARCHAR(128) NOT NULL,
  message_type VARCHAR(32) NOT NULL,
  target_scope VARCHAR(64) NOT NULL,
  target_filter_json JSON NOT NULL,
  template_snapshot_json JSON NOT NULL,
  status VARCHAR(32) NOT NULL,
  attempt_count INT NOT NULL DEFAULT 0,
  cursor_user_id BIGINT NOT NULL DEFAULT 0,
  total_count BIGINT NOT NULL DEFAULT 0,
  success_count BIGINT NOT NULL DEFAULT 0,
  failure_count BIGINT NOT NULL DEFAULT 0,
  batch_size INT NOT NULL DEFAULT 500,
  next_run_at_ms BIGINT NOT NULL DEFAULT 0,
  locked_by VARCHAR(96) NOT NULL DEFAULT '',
  locked_until_ms BIGINT NOT NULL DEFAULT 0,
  error_message VARCHAR(512) NOT NULL DEFAULT '',
  created_by VARCHAR(64) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, job_id),
  UNIQUE KEY uk_message_fanout_command (app_code, command_id),
  KEY idx_message_fanout_status (app_code, status, next_run_at_ms, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
