SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE DATABASE IF NOT EXISTS hyapp_room
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

USE hyapp_room;

CREATE TABLE IF NOT EXISTS rooms (
  room_id VARCHAR(64) NOT NULL PRIMARY KEY,
  owner_user_id BIGINT NOT NULL,
  host_user_id BIGINT NOT NULL,
  seat_count INT NOT NULL,
  mode VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  visible_region_id BIGINT NOT NULL DEFAULT 0,
  created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  UNIQUE KEY uk_rooms_owner_user (owner_user_id),
  KEY idx_rooms_region_status (visible_region_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS room_list_entries (
  room_id VARCHAR(64) NOT NULL PRIMARY KEY,
  visible_region_id BIGINT NOT NULL DEFAULT 0,
  owner_user_id BIGINT NOT NULL,
  host_user_id BIGINT NOT NULL,
  title VARCHAR(128) NOT NULL DEFAULT '',
  cover_url VARCHAR(512) NOT NULL DEFAULT '',
  mode VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  heat BIGINT NOT NULL DEFAULT 0,
  online_count INT NOT NULL DEFAULT 0,
  seat_count INT NOT NULL DEFAULT 0,
  occupied_seat_count INT NOT NULL DEFAULT 0,
  sort_score BIGINT NOT NULL DEFAULT 0,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  KEY idx_room_list_region_hot (visible_region_id, status, sort_score DESC, room_id),
  KEY idx_room_list_region_new (visible_region_id, status, created_at_ms DESC, room_id),
  KEY idx_room_list_owner (owner_user_id, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS room_snapshots (
  room_id VARCHAR(64) NOT NULL PRIMARY KEY,
  room_version BIGINT NOT NULL,
  payload LONGBLOB NOT NULL,
  created_at TIMESTAMP(3) NOT NULL,
  updated_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  INDEX idx_room_snapshots_version (room_id, room_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS room_command_log (
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  room_id VARCHAR(64) NOT NULL,
  room_version BIGINT NOT NULL,
  command_id VARCHAR(128) NOT NULL,
  command_type VARCHAR(64) NOT NULL,
  payload LONGBLOB NOT NULL,
  replayable BOOLEAN NOT NULL,
  created_at TIMESTAMP(3) NOT NULL,
  UNIQUE KEY uk_room_command (room_id, command_id),
  KEY idx_room_command_replay (room_id, room_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS room_outbox (
  event_id VARCHAR(128) NOT NULL PRIMARY KEY,
  event_type VARCHAR(64) NOT NULL,
  room_id VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  envelope LONGBLOB NOT NULL,
  created_at TIMESTAMP(3) NOT NULL,
  retry_count INT NOT NULL DEFAULT 0,
  last_error TEXT NULL,
  updated_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  KEY idx_room_outbox_pending (status, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
