CREATE TABLE IF NOT EXISTS sys_game_list_vendor_ext (
  id BIGINT NOT NULL PRIMARY KEY,
  game_list_config_id BIGINT NOT NULL,
  sys_origin VARCHAR(32) NOT NULL,
  vendor_type VARCHAR(32) NOT NULL,
  vendor_game_id VARCHAR(64) NOT NULL,
  launch_mode VARCHAR(32) NOT NULL DEFAULT 'H5_REMOTE',
  package_version VARCHAR(32) DEFAULT NULL,
  package_url VARCHAR(1024) DEFAULT NULL,
  preview_url VARCHAR(1024) DEFAULT NULL,
  orientation TINYINT DEFAULT NULL,
  safe_height INT NOT NULL DEFAULT 0,
  gsp VARCHAR(64) DEFAULT NULL,
  bridge_schema_version VARCHAR(16) NOT NULL DEFAULT '1.0',
  currency_type INT DEFAULT NULL,
  currency_icon VARCHAR(1024) DEFAULT NULL,
  extra_json TEXT DEFAULT NULL,
  enabled TINYINT(1) NOT NULL DEFAULT 1,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_game_vendor_ext_cfg_vendor (game_list_config_id, vendor_type),
  KEY idx_game_vendor_ext_sys_vendor (sys_origin, vendor_type, enabled)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS baishun_game_catalog (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL,
  internal_game_id VARCHAR(64) NOT NULL,
  vendor_game_id INT NOT NULL,
  name VARCHAR(128) NOT NULL,
  cover VARCHAR(1024) DEFAULT NULL,
  preview_url VARCHAR(1024) DEFAULT NULL,
  download_url VARCHAR(1024) DEFAULT NULL,
  package_version VARCHAR(32) DEFAULT NULL,
  game_mode_json VARCHAR(64) DEFAULT NULL,
  orientation TINYINT DEFAULT NULL,
  safe_height INT NOT NULL DEFAULT 0,
  venue_level_json VARCHAR(64) DEFAULT NULL,
  gsp VARCHAR(64) DEFAULT NULL,
  raw_json LONGTEXT DEFAULT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'ENABLED',
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_baishun_catalog_sys_vendor_game (sys_origin, vendor_game_id),
  KEY idx_baishun_catalog_internal_game (sys_origin, internal_game_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS baishun_launch_session (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL,
  room_id VARCHAR(64) NOT NULL,
  user_id BIGINT NOT NULL,
  internal_game_id VARCHAR(64) NOT NULL,
  vendor_game_id INT NOT NULL,
  game_session_id VARCHAR(64) NOT NULL,
  launch_code VARCHAR(128) NOT NULL,
  launch_code_expire_time DATETIME NOT NULL,
  ss_token VARCHAR(255) DEFAULT NULL,
  ss_token_expire_time DATETIME DEFAULT NULL,
  language VARCHAR(16) NOT NULL DEFAULT '0',
  gsp INT NOT NULL DEFAULT 0,
  currency_type INT NOT NULL DEFAULT 0,
  currency_icon VARCHAR(1024) DEFAULT NULL,
  scene_mode INT NOT NULL DEFAULT 0,
  game_mode INT NOT NULL DEFAULT 3,
  package_version VARCHAR(32) DEFAULT NULL,
  client_ip VARCHAR(64) DEFAULT NULL,
  client_origin VARCHAR(16) DEFAULT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'INIT',
  closed_reason VARCHAR(64) DEFAULT NULL,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_baishun_launch_code (launch_code),
  UNIQUE KEY uk_baishun_ss_token (ss_token),
  UNIQUE KEY uk_baishun_game_session (game_session_id),
  KEY idx_baishun_session_room_user (sys_origin, room_id, user_id, status),
  KEY idx_baishun_session_token_expire (ss_token_expire_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS baishun_order_idempotency (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL,
  order_id VARCHAR(128) NOT NULL,
  game_round_id VARCHAR(128) DEFAULT NULL,
  room_id VARCHAR(64) NOT NULL,
  user_id BIGINT NOT NULL,
  vendor_game_id INT NOT NULL,
  currency_diff BIGINT NOT NULL DEFAULT 0,
  diff_msg VARCHAR(32) NOT NULL,
  msg_type VARCHAR(64) DEFAULT NULL,
  currency_type INT DEFAULT NULL,
  wallet_event_id VARCHAR(160) NOT NULL,
  wallet_asset_record_id BIGINT DEFAULT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'INIT',
  request_json LONGTEXT DEFAULT NULL,
  response_json LONGTEXT DEFAULT NULL,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_baishun_order (sys_origin, order_id),
  UNIQUE KEY uk_baishun_wallet_event (wallet_event_id),
  KEY idx_baishun_round (game_round_id),
  KEY idx_baishun_order_user_time (sys_origin, user_id, create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS baishun_callback_log (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) DEFAULT NULL,
  request_type VARCHAR(32) NOT NULL,
  order_id VARCHAR(128) DEFAULT NULL,
  game_round_id VARCHAR(128) DEFAULT NULL,
  room_id VARCHAR(64) DEFAULT NULL,
  user_id BIGINT DEFAULT NULL,
  vendor_game_id INT DEFAULT NULL,
  request_json LONGTEXT DEFAULT NULL,
  response_json LONGTEXT DEFAULT NULL,
  biz_code VARCHAR(32) DEFAULT NULL,
  biz_message TEXT DEFAULT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'SUCCESS',
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_baishun_log_type_time (request_type, create_time),
  KEY idx_baishun_log_order (order_id),
  KEY idx_baishun_log_room_user (room_id, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS baishun_room_state (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL,
  room_id VARCHAR(64) NOT NULL,
  host_user_id BIGINT NOT NULL,
  current_game_id VARCHAR(64) DEFAULT NULL,
  current_vendor_game_id INT DEFAULT NULL,
  current_game_name VARCHAR(128) DEFAULT NULL,
  current_game_cover VARCHAR(1024) DEFAULT NULL,
  game_session_id VARCHAR(64) DEFAULT NULL,
  launch_user_id BIGINT DEFAULT NULL,
  state VARCHAR(32) NOT NULL DEFAULT 'IDLE',
  extra_json TEXT DEFAULT NULL,
  start_time DATETIME DEFAULT NULL,
  end_time DATETIME DEFAULT NULL,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_baishun_room_state (sys_origin, room_id),
  KEY idx_baishun_room_state_session (game_session_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
