SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

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

USE hyapp_game;

CREATE TABLE IF NOT EXISTS game_platforms (
  app_code VARCHAR(32) NOT NULL,
  platform_code VARCHAR(64) NOT NULL,
  platform_name VARCHAR(128) NOT NULL,
  status VARCHAR(32) NOT NULL,
  api_base_url VARCHAR(512) NOT NULL DEFAULT '',
  callback_secret_ciphertext VARCHAR(1024) NOT NULL DEFAULT '',
  callback_ip_whitelist JSON NULL,
  adapter_config JSON NULL,
  sort_order INT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY(app_code, platform_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS game_catalog (
  app_code VARCHAR(32) NOT NULL,
  game_id VARCHAR(96) NOT NULL,
  platform_code VARCHAR(64) NOT NULL,
  provider_game_id VARCHAR(128) NOT NULL,
  game_name VARCHAR(128) NOT NULL,
  category VARCHAR(64) NOT NULL DEFAULT '',
  icon_url VARCHAR(512) NOT NULL DEFAULT '',
  cover_url VARCHAR(512) NOT NULL DEFAULT '',
  launch_mode VARCHAR(32) NOT NULL DEFAULT 'h5_popup',
  orientation VARCHAR(32) NOT NULL DEFAULT 'portrait',
  min_coin BIGINT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  tags JSON NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY(app_code, game_id),
  UNIQUE KEY uk_game_provider(app_code, platform_code, provider_game_id),
  KEY idx_game_platform_status(app_code, platform_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS game_display_rules (
  app_code VARCHAR(32) NOT NULL,
  rule_id VARCHAR(96) NOT NULL,
  game_id VARCHAR(96) NOT NULL,
  scene VARCHAR(64) NOT NULL,
  region_id BIGINT NOT NULL DEFAULT 0,
  language VARCHAR(32) NOT NULL DEFAULT '',
  platform VARCHAR(16) NOT NULL DEFAULT '',
  visible TINYINT(1) NOT NULL DEFAULT 1,
  enabled TINYINT(1) NOT NULL DEFAULT 1,
  sort_order INT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY(app_code, rule_id),
  KEY idx_game_display(app_code, scene, region_id, visible, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS game_launch_sessions (
  app_code VARCHAR(32) NOT NULL,
  session_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  display_user_id VARCHAR(32) NOT NULL,
  room_id VARCHAR(96) NOT NULL DEFAULT '',
  scene VARCHAR(64) NOT NULL,
  platform_code VARCHAR(64) NOT NULL,
  game_id VARCHAR(96) NOT NULL,
  provider_game_id VARCHAR(128) NOT NULL,
  launch_token_hash VARCHAR(128) NOT NULL,
  status VARCHAR(32) NOT NULL,
  expires_at_ms BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY(app_code, session_id),
  KEY idx_game_launch_user(app_code, user_id, created_at_ms),
  KEY idx_game_launch_token(app_code, launch_token_hash)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS game_orders (
  app_code VARCHAR(32) NOT NULL,
  order_id VARCHAR(96) NOT NULL,
  platform_code VARCHAR(64) NOT NULL,
  provider_order_id VARCHAR(160) NOT NULL,
  provider_round_id VARCHAR(160) NOT NULL DEFAULT '',
  game_id VARCHAR(96) NOT NULL,
  provider_game_id VARCHAR(128) NOT NULL,
  user_id BIGINT NOT NULL,
  room_id VARCHAR(96) NOT NULL DEFAULT '',
  op_type VARCHAR(32) NOT NULL,
  coin_amount BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  wallet_transaction_id VARCHAR(96) NOT NULL DEFAULT '',
  wallet_balance_after BIGINT NOT NULL DEFAULT 0,
  request_hash VARCHAR(128) NOT NULL,
  raw_payload_ref VARCHAR(256) NOT NULL DEFAULT '',
  failure_code VARCHAR(64) NOT NULL DEFAULT '',
  failure_message VARCHAR(256) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY(app_code, order_id),
  UNIQUE KEY uk_game_provider_order(app_code, platform_code, provider_order_id),
  KEY idx_game_order_user_time(app_code, user_id, created_at_ms),
  KEY idx_game_order_round(app_code, platform_code, provider_round_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS game_callback_logs (
  app_code VARCHAR(32) NOT NULL,
  callback_id VARCHAR(96) NOT NULL,
  platform_code VARCHAR(64) NOT NULL,
  operation VARCHAR(64) NOT NULL,
  request_id VARCHAR(96) NOT NULL DEFAULT '',
  provider_request_id VARCHAR(160) NOT NULL DEFAULT '',
  signature_valid TINYINT(1) NOT NULL DEFAULT 0,
  request_hash VARCHAR(128) NOT NULL,
  response_code VARCHAR(64) NOT NULL DEFAULT '',
  response_body_hash VARCHAR(128) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL,
  created_at_ms BIGINT NOT NULL,
  PRIMARY KEY(app_code, callback_id),
  KEY idx_game_callback_platform_time(app_code, platform_code, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS game_repair_orders (
  app_code VARCHAR(32) NOT NULL,
  repair_id VARCHAR(96) NOT NULL,
  platform_code VARCHAR(64) NOT NULL,
  provider_order_id VARCHAR(160) NOT NULL,
  reason VARCHAR(128) NOT NULL,
  status VARCHAR(32) NOT NULL,
  operator_admin_id BIGINT NOT NULL DEFAULT 0,
  attempts INT NOT NULL DEFAULT 0,
  next_run_at_ms BIGINT NOT NULL DEFAULT 0,
  last_error_code VARCHAR(64) NOT NULL DEFAULT '',
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY(app_code, repair_id),
  UNIQUE KEY uk_game_repair_provider_order(app_code, platform_code, provider_order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS game_level_event_outbox (
  app_code VARCHAR(32) NOT NULL,
  event_id VARCHAR(160) NOT NULL,
  order_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  game_id VARCHAR(96) NOT NULL,
  provider_order_id VARCHAR(160) NOT NULL,
  provider_round_id VARCHAR(160) NOT NULL DEFAULT '',
  coin_amount BIGINT NOT NULL,
  wallet_transaction_id VARCHAR(96) NOT NULL,
  payload_json JSON NULL,
  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 '',
  occurred_at_ms BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY(app_code, event_id),
  UNIQUE KEY uk_game_level_event_order(app_code, order_id),
  KEY idx_game_level_event_pending(app_code, status, next_retry_at_ms, created_at_ms),
  KEY idx_game_level_event_user(app_code, user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO game_platforms (
  app_code, platform_code, platform_name, status, api_base_url, sort_order, created_at_ms, updated_at_ms
) VALUES (
  'lalu', 'demo', 'Demo Games', 'active', 'https://game.example.local/h5', 10, 0, 0
) ON DUPLICATE KEY UPDATE platform_name = VALUES(platform_name);

INSERT INTO game_catalog (
  app_code, game_id, platform_code, provider_game_id, game_name, category, icon_url, cover_url,
  launch_mode, orientation, min_coin, status, sort_order, tags, created_at_ms, updated_at_ms
) VALUES (
  'lalu', 'demo_rocket_001', 'demo', 'rocket_001', 'Rocket', 'casual',
  'https://cdn.example.com/game/rocket.png', 'https://cdn.example.com/game/rocket-cover.png',
  'h5_popup', 'portrait', 100, 'active', 10, JSON_ARRAY('demo','voice_room'), 0, 0
) ON DUPLICATE KEY UPDATE game_name = VALUES(game_name);

INSERT INTO game_display_rules (
  app_code, rule_id, game_id, scene, region_id, language, platform, visible, enabled, sort_order, created_at_ms, updated_at_ms
) VALUES (
  'lalu', 'demo_rocket_voice_default', 'demo_rocket_001', 'voice_room', 0, '', '', 1, 1, 10, 0, 0
) ON DUPLICATE KEY UPDATE visible = VALUES(visible), enabled = VALUES(enabled);
