SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

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

USE hyapp_wallet;

CREATE TABLE IF NOT EXISTS wallet_accounts (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  asset_type VARCHAR(32) NOT NULL,
  available_amount BIGINT NOT NULL,
  frozen_amount BIGINT NOT NULL,
  version BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id, asset_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_transactions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  transaction_id VARCHAR(96) NOT NULL,
  command_id VARCHAR(128) NOT NULL,
  biz_type VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  request_hash VARCHAR(128) NOT NULL,
  external_ref VARCHAR(128) NOT NULL DEFAULT '',
  metadata_json JSON NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, transaction_id),
  UNIQUE KEY uk_wallet_tx_command (app_code, command_id),
  KEY idx_wallet_tx_biz_time (app_code, biz_type, created_at_ms),
  KEY idx_wallet_tx_external_ref (app_code, external_ref)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_entries (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  entry_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  transaction_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  asset_type VARCHAR(32) NOT NULL,
  available_delta BIGINT NOT NULL,
  frozen_delta BIGINT NOT NULL,
  available_after BIGINT NOT NULL,
  frozen_after BIGINT NOT NULL,
  counterparty_user_id BIGINT NOT NULL DEFAULT 0,
  room_id VARCHAR(96) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  KEY idx_wallet_entries_user_time (app_code, user_id, created_at_ms),
  KEY idx_wallet_entries_tx (app_code, transaction_id),
  KEY idx_wallet_entries_asset_time (app_code, asset_type, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_outbox (
  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,
  command_id VARCHAR(128) NOT NULL,
  user_id BIGINT NOT NULL,
  asset_type VARCHAR(32) NOT NULL,
  available_delta BIGINT NOT NULL,
  frozen_delta BIGINT NOT NULL,
  payload JSON NOT NULL,
  status VARCHAR(32) NOT NULL,
  retry_count INT NOT NULL DEFAULT 0,
  last_error TEXT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, event_id),
  KEY idx_wallet_outbox_pending (app_code, status, created_at_ms),
  KEY idx_wallet_outbox_tx (app_code, transaction_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_gift_prices (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  gift_id VARCHAR(96) NOT NULL,
  price_version VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  charge_asset_type VARCHAR(32) NOT NULL DEFAULT 'COIN',
  coin_price BIGINT NOT NULL,
  gift_point_amount BIGINT NOT NULL,
  heat_value BIGINT NOT NULL,
  effective_at_ms BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, gift_id, price_version),
  KEY idx_wallet_gift_prices_effective (app_code, gift_id, status, effective_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_recharge_policies (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  policy_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  region_id BIGINT NOT NULL,
  policy_version VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  currency_code VARCHAR(3) NOT NULL DEFAULT 'USD',
  coin_amount BIGINT NOT NULL,
  usd_minor_amount BIGINT 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,
  KEY idx_wallet_recharge_policies_active (app_code, region_id, status, effective_from_ms),
  UNIQUE KEY uk_wallet_recharge_policy_version (app_code, region_id, policy_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_recharge_records (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  transaction_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  seller_user_id BIGINT NOT NULL,
  seller_region_id BIGINT NOT NULL,
  target_region_id BIGINT NOT NULL,
  policy_id BIGINT NOT NULL,
  policy_version VARCHAR(64) NOT NULL,
  currency_code VARCHAR(3) NOT NULL,
  coin_amount BIGINT NOT NULL,
  usd_minor_amount BIGINT NOT NULL,
  exchange_coin_amount BIGINT NOT NULL,
  exchange_usd_minor_amount BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, transaction_id),
  KEY idx_wallet_recharge_records_user_time (app_code, user_id, created_at_ms),
  KEY idx_wallet_recharge_records_seller_time (app_code, seller_user_id, created_at_ms),
  KEY idx_wallet_recharge_records_region_time (app_code, target_region_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_recharge_products (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  product_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  product_code VARCHAR(128) NULL,
  product_name VARCHAR(128) NOT NULL,
  description VARCHAR(512) NOT NULL DEFAULT '',
  platform VARCHAR(16) NOT NULL,
  channel VARCHAR(32) NOT NULL,
  currency_code VARCHAR(8) NOT NULL DEFAULT 'USDT',
  amount_micro BIGINT NOT NULL,
  coin_amount BIGINT NOT NULL,
  resource_asset_type VARCHAR(32) NOT NULL DEFAULT 'COIN',
  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_wallet_recharge_products_code (app_code, product_code),
  KEY idx_wallet_recharge_products_scope (app_code, platform, status, amount_micro, product_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_recharge_product_regions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  product_id BIGINT NOT NULL,
  region_id BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, product_id, region_id),
  KEY idx_wallet_recharge_product_regions_region (app_code, region_id, product_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS coin_seller_stock_records (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  stock_id VARCHAR(96) NOT NULL,
  transaction_id VARCHAR(96) NOT NULL,
  command_id VARCHAR(128) NOT NULL,
  seller_user_id BIGINT NOT NULL,
  stock_type VARCHAR(32) NOT NULL,
  counts_as_seller_recharge BOOLEAN NOT NULL,
  coin_amount BIGINT NOT NULL,
  paid_currency_code VARCHAR(8) NOT NULL DEFAULT '',
  paid_amount_micro BIGINT NOT NULL DEFAULT 0,
  payment_ref VARCHAR(128) NULL,
  evidence_ref VARCHAR(256) NOT NULL DEFAULT '',
  operator_user_id BIGINT NOT NULL,
  reason VARCHAR(512) NOT NULL DEFAULT '',
  balance_after BIGINT NOT NULL,
  metadata_json JSON NULL,
  created_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stock_id),
  UNIQUE KEY uk_coin_seller_stock_command (app_code, command_id),
  UNIQUE KEY uk_coin_seller_stock_payment_ref (app_code, stock_type, payment_ref),
  KEY idx_coin_seller_stock_seller_time (app_code, seller_user_id, created_at_ms),
  KEY idx_coin_seller_stock_type_time (app_code, stock_type, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS resources (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  resource_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  resource_code VARCHAR(96) NOT NULL,
  resource_type VARCHAR(32) NOT NULL,
  name VARCHAR(128) NOT NULL,
  status VARCHAR(32) NOT NULL,
  grantable BOOLEAN NOT NULL DEFAULT TRUE,
  manager_grant_enabled BOOLEAN NOT NULL DEFAULT TRUE,
  grant_strategy VARCHAR(32) NOT NULL,
  wallet_asset_type VARCHAR(32) NOT NULL DEFAULT '',
  wallet_asset_amount BIGINT NOT NULL DEFAULT 0,
  usage_scope_json JSON NOT NULL,
  asset_url VARCHAR(512) NOT NULL DEFAULT '',
  preview_url VARCHAR(512) NOT NULL DEFAULT '',
  animation_url VARCHAR(512) NOT NULL DEFAULT '',
  metadata_json JSON NULL,
  sort_order INT NOT NULL DEFAULT 0,
  created_by_user_id BIGINT NOT NULL DEFAULT 0,
  updated_by_user_id BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_resources_code (app_code, resource_code),
  KEY idx_resources_type_status (app_code, resource_type, status, sort_order),
  KEY idx_resources_manager_grant (app_code, manager_grant_enabled, status, grantable, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'resources' AND COLUMN_NAME = 'manager_grant_enabled') = 0,
  'ALTER TABLE resources ADD COLUMN manager_grant_enabled BOOLEAN NOT NULL DEFAULT TRUE AFTER grantable',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'resources' AND INDEX_NAME = 'idx_resources_manager_grant') = 0,
  'ALTER TABLE resources ADD KEY idx_resources_manager_grant (app_code, manager_grant_enabled, status, grantable, sort_order)',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS resource_groups (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  group_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  group_code VARCHAR(96) NOT NULL,
  name VARCHAR(128) NOT NULL,
  status VARCHAR(32) NOT NULL,
  description VARCHAR(512) NOT NULL DEFAULT '',
  sort_order INT NOT NULL DEFAULT 0,
  created_by_user_id BIGINT NOT NULL DEFAULT 0,
  updated_by_user_id BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_resource_groups_code (app_code, group_code),
  KEY idx_resource_groups_status (app_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS resource_group_items (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  group_item_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  group_id BIGINT NOT NULL,
  item_type VARCHAR(32) NOT NULL DEFAULT 'resource',
  resource_id BIGINT NOT NULL DEFAULT 0,
  wallet_asset_type VARCHAR(32) NOT NULL DEFAULT '',
  wallet_asset_amount BIGINT NOT NULL DEFAULT 0,
  quantity BIGINT NOT NULL DEFAULT 1,
  duration_ms BIGINT NOT NULL DEFAULT 0,
  sort_order INT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_resource_group_items_member (app_code, group_id, item_type, resource_id, wallet_asset_type),
  KEY idx_resource_group_items_group (app_code, group_id, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS gift_configs (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  gift_id VARCHAR(96) NOT NULL,
  resource_id BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  name VARCHAR(128) NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  presentation_json JSON NULL,
  gift_type_code VARCHAR(32) NOT NULL DEFAULT 'normal',
  effective_from_ms BIGINT NOT NULL DEFAULT 0,
  effective_to_ms BIGINT NOT NULL DEFAULT 0,
  effect_types_json JSON NULL,
  created_by_user_id BIGINT NOT NULL DEFAULT 0,
  updated_by_user_id BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, gift_id),
  KEY idx_gift_configs_resource (app_code, resource_id),
  KEY idx_gift_configs_status_sort (app_code, status, sort_order),
  KEY idx_gift_configs_effective (app_code, status, effective_from_ms, effective_to_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_gift_prices' AND COLUMN_NAME = 'charge_asset_type') = 0,
  'ALTER TABLE wallet_gift_prices ADD COLUMN charge_asset_type VARCHAR(32) NOT NULL DEFAULT ''COIN'' AFTER status',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'gift_configs' AND COLUMN_NAME = 'gift_type_code') = 0,
  'ALTER TABLE gift_configs ADD COLUMN gift_type_code VARCHAR(32) NOT NULL DEFAULT ''normal'' AFTER presentation_json',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'gift_configs' AND COLUMN_NAME = 'effective_from_ms') = 0,
  'ALTER TABLE gift_configs ADD COLUMN effective_from_ms BIGINT NOT NULL DEFAULT 0 AFTER gift_type_code',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'gift_configs' AND COLUMN_NAME = 'effective_to_ms') = 0,
  'ALTER TABLE gift_configs ADD COLUMN effective_to_ms BIGINT NOT NULL DEFAULT 0 AFTER effective_from_ms',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'gift_configs' AND COLUMN_NAME = 'effect_types_json') = 0,
  'ALTER TABLE gift_configs ADD COLUMN effect_types_json JSON NULL AFTER effective_to_ms',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS gift_config_regions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  gift_id VARCHAR(96) NOT NULL,
  region_id BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, gift_id, region_id),
  KEY idx_gift_config_regions_region (app_code, region_id, gift_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_gift_wall (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  gift_id VARCHAR(96) NOT NULL,
  gift_name VARCHAR(128) NOT NULL,
  resource_id BIGINT NOT NULL DEFAULT 0,
  resource_snapshot_json JSON NOT NULL,
  gift_type_code VARCHAR(32) NOT NULL DEFAULT 'normal',
  presentation_json JSON NULL,
  gift_count BIGINT NOT NULL DEFAULT 0,
  total_value BIGINT NOT NULL DEFAULT 0,
  total_coin_value BIGINT NOT NULL DEFAULT 0,
  total_diamond_value BIGINT NOT NULL DEFAULT 0,
  total_gift_point BIGINT NOT NULL DEFAULT 0,
  total_heat_value BIGINT NOT NULL DEFAULT 0,
  charge_asset_type VARCHAR(32) NOT NULL DEFAULT 'COIN',
  last_price_version VARCHAR(64) NOT NULL DEFAULT '',
  first_received_at_ms BIGINT NOT NULL DEFAULT 0,
  last_received_at_ms BIGINT NOT NULL DEFAULT 0,
  sort_order INT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id, gift_id),
  KEY idx_user_gift_wall_user_sort (app_code, user_id, sort_order, total_value, last_received_at_ms),
  KEY idx_user_gift_wall_gift (app_code, gift_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_projection_events (
  projection_name VARCHAR(64) NOT NULL,
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  event_id VARCHAR(128) NOT NULL,
  status VARCHAR(32) NOT NULL,
  retry_count INT NOT NULL DEFAULT 0,
  locked_by VARCHAR(96) NOT NULL DEFAULT '',
  locked_until_ms BIGINT NOT NULL DEFAULT 0,
  last_error TEXT NULL,
  processed_at_ms BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (projection_name, app_code, event_id),
  KEY idx_wallet_projection_claim (projection_name, app_code, status, locked_until_ms, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS resource_grants (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  grant_id VARCHAR(96) NOT NULL,
  command_id VARCHAR(128) NOT NULL,
  target_user_id BIGINT NOT NULL,
  grant_source VARCHAR(32) NOT NULL,
  grant_subject_type VARCHAR(32) NOT NULL,
  grant_subject_id VARCHAR(96) NOT NULL,
  status VARCHAR(32) NOT NULL,
  request_hash VARCHAR(128) NOT NULL,
  group_snapshot_json JSON NULL,
  reason VARCHAR(256) NOT NULL,
  operator_user_id BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, grant_id),
  UNIQUE KEY uk_resource_grants_command (app_code, command_id),
  KEY idx_resource_grants_target_time (app_code, target_user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS resource_grant_items (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  grant_item_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  grant_id VARCHAR(96) NOT NULL,
  resource_id BIGINT NOT NULL,
  resource_snapshot_json JSON NOT NULL,
  quantity BIGINT NOT NULL DEFAULT 1,
  duration_ms BIGINT NOT NULL DEFAULT 0,
  result_type VARCHAR(32) NOT NULL,
  wallet_transaction_id VARCHAR(96) NOT NULL DEFAULT '',
  entitlement_id VARCHAR(96) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  KEY idx_resource_grant_items_grant (app_code, grant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_resource_entitlements (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  entitlement_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  resource_id BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  quantity BIGINT NOT NULL DEFAULT 1,
  remaining_quantity BIGINT NOT NULL DEFAULT 1,
  effective_at_ms BIGINT NOT NULL,
  expires_at_ms BIGINT NOT NULL DEFAULT 0,
  source_grant_id VARCHAR(96) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, entitlement_id),
  KEY idx_user_resource_active (app_code, user_id, resource_id, status, expires_at_ms),
  KEY idx_user_resource_user_type (app_code, user_id, status, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_resource_equipment (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  resource_type VARCHAR(32) NOT NULL,
  entitlement_id VARCHAR(96) NOT NULL,
  resource_id BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id, resource_type),
  KEY idx_user_resource_equipment_user (app_code, user_id, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_diamond_exchange_rules (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  rule_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  exchange_type VARCHAR(32) NOT NULL,
  status VARCHAR(32) NOT NULL,
  from_asset_type VARCHAR(32) NOT NULL DEFAULT 'DIAMOND',
  to_asset_type VARCHAR(32) NOT NULL,
  from_amount BIGINT NOT NULL,
  to_amount BIGINT NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_diamond_exchange_rule (app_code, exchange_type, from_amount, to_asset_type),
  KEY idx_diamond_exchange_active (app_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wallet_withdrawal_requests (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  withdrawal_id VARCHAR(96) NOT NULL,
  command_id VARCHAR(128) NOT NULL,
  user_id BIGINT NOT NULL,
  asset_type VARCHAR(32) NOT NULL,
  amount BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  payout_account VARCHAR(256) NOT NULL,
  reason VARCHAR(512) NOT NULL DEFAULT '',
  wallet_transaction_id VARCHAR(96) 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,
  PRIMARY KEY (app_code, withdrawal_id),
  UNIQUE KEY uk_withdrawal_command (app_code, command_id),
  KEY idx_withdrawal_user_time (app_code, user_id, created_at_ms),
  KEY idx_withdrawal_status_time (app_code, status, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS vip_levels (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  level INT NOT NULL,
  name VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  price_coin BIGINT NOT NULL,
  duration_ms BIGINT NOT NULL,
  reward_resource_group_id BIGINT NOT NULL DEFAULT 0,
  sort_order INT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, level),
  KEY idx_vip_levels_status_sort (app_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_vip_memberships (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  user_id BIGINT NOT NULL,
  level INT NOT NULL,
  name VARCHAR(64) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL,
  started_at_ms BIGINT NOT NULL DEFAULT 0,
  expires_at_ms BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id),
  KEY idx_user_vip_expiry (app_code, status, expires_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS vip_purchase_orders (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  order_id VARCHAR(96) NOT NULL,
  command_id VARCHAR(128) NOT NULL,
  user_id BIGINT NOT NULL,
  target_level INT NOT NULL,
  previous_level INT NOT NULL,
  price_coin BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  wallet_transaction_id VARCHAR(96) NOT NULL,
  resource_grant_id VARCHAR(96) NOT NULL DEFAULT '',
  request_hash VARCHAR(128) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, order_id),
  UNIQUE KEY uk_vip_purchase_command (app_code, command_id),
  KEY idx_vip_purchase_user_time (app_code, user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS user_vip_history (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu',
  history_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id BIGINT NOT NULL,
  action VARCHAR(32) NOT NULL,
  previous_level INT NOT NULL,
  new_level INT NOT NULL,
  order_id VARCHAR(96) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  KEY idx_user_vip_history_user_time (app_code, user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO wallet_gift_prices (
  app_code, gift_id, price_version, status, charge_asset_type, coin_price, gift_point_amount, heat_value,
  effective_at_ms, created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'rose', 'v1', 'active', 'COIN', 10, 10, 10, 0, 0, 0),
  ('lalu', 'rocket', 'v1', 'active', 'COIN', 100, 100, 100, 0, 0, 0);

INSERT IGNORE INTO resources (
  app_code, resource_code, resource_type, name, status, grantable, grant_strategy,
  wallet_asset_type, wallet_asset_amount, usage_scope_json, asset_url, preview_url,
  animation_url, metadata_json, sort_order, created_by_user_id, updated_by_user_id,
  created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'gift_rose', 'gift', 'Rose', 'active', TRUE, 'increase_quantity', '', 0, JSON_ARRAY('gift'), '', '', '', JSON_OBJECT(), 10, 0, 0, 0, 0),
  ('lalu', 'gift_rocket', 'gift', 'Rocket', 'active', TRUE, 'increase_quantity', '', 0, JSON_ARRAY('gift'), '', '', '', JSON_OBJECT(), 20, 0, 0, 0, 0);

INSERT IGNORE INTO resources (
  app_code, resource_code, resource_type, name, status, grantable, grant_strategy,
  wallet_asset_type, wallet_asset_amount, usage_scope_json, asset_url, preview_url,
  animation_url, metadata_json, sort_order, created_by_user_id, updated_by_user_id,
  created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'vip1_avatar_frame', 'avatar_frame', 'VIP1 Avatar Frame', 'active', TRUE, 'set_active_flag', '', 0, JSON_ARRAY('profile', 'room'), '', '', '', JSON_OBJECT(), 101, 0, 0, 0, 0),
  ('lalu', 'vip2_avatar_frame', 'avatar_frame', 'VIP2 Avatar Frame', 'active', TRUE, 'set_active_flag', '', 0, JSON_ARRAY('profile', 'room'), '', '', '', JSON_OBJECT(), 102, 0, 0, 0, 0),
  ('lalu', 'vip3_vehicle', 'vehicle', 'VIP3 Vehicle', 'active', TRUE, 'set_active_flag', '', 0, JSON_ARRAY('room_entry'), '', '', '', JSON_OBJECT(), 103, 0, 0, 0, 0);

INSERT IGNORE INTO resource_groups (
  app_code, group_code, name, status, description, sort_order,
  created_by_user_id, updated_by_user_id, created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'vip1_rewards', 'VIP1 Rewards', 'active', 'VIP1 purchase rewards', 101, 0, 0, 0, 0),
  ('lalu', 'vip2_rewards', 'VIP2 Rewards', 'active', 'VIP2 purchase rewards', 102, 0, 0, 0, 0),
  ('lalu', 'vip3_rewards', 'VIP3 Rewards', 'active', 'VIP3 purchase rewards', 103, 0, 0, 0, 0);

INSERT IGNORE INTO resource_group_items (
  app_code, group_id, item_type, resource_id, wallet_asset_type, wallet_asset_amount,
  quantity, duration_ms, sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', g.group_id, 'resource', r.resource_id, '', 0, 1, 604800000, 10, 0, 0
FROM resource_groups g
JOIN resources r ON r.app_code = g.app_code AND r.resource_code = 'vip1_avatar_frame'
WHERE g.app_code = 'lalu' AND g.group_code = 'vip1_rewards';

INSERT IGNORE INTO resource_group_items (
  app_code, group_id, item_type, resource_id, wallet_asset_type, wallet_asset_amount,
  quantity, duration_ms, sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', g.group_id, 'resource', r.resource_id, '', 0, 1, 604800000, 10, 0, 0
FROM resource_groups g
JOIN resources r ON r.app_code = g.app_code AND r.resource_code = 'vip2_avatar_frame'
WHERE g.app_code = 'lalu' AND g.group_code = 'vip2_rewards';

INSERT IGNORE INTO resource_group_items (
  app_code, group_id, item_type, resource_id, wallet_asset_type, wallet_asset_amount,
  quantity, duration_ms, sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', g.group_id, 'resource', r.resource_id, '', 0, 1, 604800000, 10, 0, 0
FROM resource_groups g
JOIN resources r ON r.app_code = g.app_code AND r.resource_code = 'vip3_vehicle'
WHERE g.app_code = 'lalu' AND g.group_code = 'vip3_rewards';

INSERT IGNORE INTO vip_levels (
  app_code, level, name, status, price_coin, duration_ms, reward_resource_group_id,
  sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', 1, 'VIP1', 'active', 1000, 604800000, group_id, 10, 0, 0
FROM resource_groups
WHERE app_code = 'lalu' AND group_code = 'vip1_rewards';

INSERT IGNORE INTO vip_levels (
  app_code, level, name, status, price_coin, duration_ms, reward_resource_group_id,
  sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', 2, 'VIP2', 'active', 3000, 604800000, group_id, 20, 0, 0
FROM resource_groups
WHERE app_code = 'lalu' AND group_code = 'vip2_rewards';

INSERT IGNORE INTO vip_levels (
  app_code, level, name, status, price_coin, duration_ms, reward_resource_group_id,
  sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', 3, 'VIP3', 'active', 8000, 604800000, group_id, 30, 0, 0
FROM resource_groups
WHERE app_code = 'lalu' AND group_code = 'vip3_rewards';

INSERT IGNORE INTO wallet_diamond_exchange_rules (
  app_code, exchange_type, status, from_asset_type, to_asset_type,
  from_amount, to_amount, sort_order, created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'diamond_to_coin', 'active', 'DIAMOND', 'COIN', 100, 100, 10, 0, 0),
  ('lalu', 'diamond_to_usd_balance', 'active', 'DIAMOND', 'USD_BALANCE', 1000, 100, 20, 0, 0);

INSERT IGNORE INTO gift_configs (
  app_code, gift_id, resource_id, status, name, sort_order, presentation_json,
  gift_type_code, effective_from_ms, effective_to_ms, effect_types_json,
  created_by_user_id, updated_by_user_id, created_at_ms, updated_at_ms
)
SELECT 'lalu', 'rose', resource_id, 'active', 'Rose', 10, JSON_OBJECT(),
       'normal', 0, 0, JSON_ARRAY(),
       0, 0, 0, 0
FROM resources
WHERE app_code = 'lalu' AND resource_code = 'gift_rose'
LIMIT 1;

INSERT IGNORE INTO gift_configs (
  app_code, gift_id, resource_id, status, name, sort_order, presentation_json,
  gift_type_code, effective_from_ms, effective_to_ms, effect_types_json,
  created_by_user_id, updated_by_user_id, created_at_ms, updated_at_ms
)
SELECT 'lalu', 'rocket', resource_id, 'active', 'Rocket', 20, JSON_OBJECT(),
       'normal', 0, 0, JSON_ARRAY('animation', 'global_broadcast'),
       0, 0, 0, 0
FROM resources
WHERE app_code = 'lalu' AND resource_code = 'gift_rocket'
LIMIT 1;

INSERT IGNORE INTO gift_config_regions (
  app_code, gift_id, region_id, created_at_ms, updated_at_ms
)
SELECT gc.app_code, gc.gift_id, 0, 0, 0
FROM gift_configs gc
LEFT JOIN gift_config_regions existing
  ON existing.app_code = gc.app_code AND existing.gift_id = gc.gift_id
WHERE existing.gift_id IS NULL
  AND gc.app_code = 'lalu'
  AND gc.gift_id IN ('rose', 'rocket');
