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 (
  app_code VARCHAR(32) NOT NULL,
  room_id VARCHAR(64) NOT NULL,
  room_short_id VARCHAR(32) NOT NULL DEFAULT '',
  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_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, room_id),
  UNIQUE KEY uk_rooms_short_id (app_code, room_short_id),
  UNIQUE KEY uk_rooms_owner_user (app_code, owner_user_id),
  KEY idx_rooms_region_status (app_code, visible_region_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS room_list_entries (
  app_code VARCHAR(32) NOT NULL,
  room_id VARCHAR(64) NOT NULL,
  room_short_id VARCHAR(32) NOT NULL DEFAULT '',
  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,
  PRIMARY KEY (app_code, room_id),
  KEY idx_room_list_short_id (app_code, room_short_id),
  KEY idx_room_list_region_hot (app_code, visible_region_id, status, sort_score DESC, room_id),
  KEY idx_room_list_region_new (app_code, visible_region_id, status, created_at_ms DESC, room_id),
  KEY idx_room_list_owner (app_code, owner_user_id, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS room_user_presence (
  app_code VARCHAR(32) NOT NULL,
  user_id BIGINT NOT NULL,
  room_id VARCHAR(64) NOT NULL,
  role VARCHAR(32) NOT NULL,
  publish_state VARCHAR(32) NOT NULL DEFAULT '',
  mic_session_id VARCHAR(128) NOT NULL DEFAULT '',
  room_version BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL,
  last_seen_at_ms BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id),
  KEY idx_room_user_presence_room (app_code, room_id, status),
  KEY idx_room_user_presence_updated (app_code, status, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS room_snapshots (
  app_code VARCHAR(32) NOT NULL,
  room_id VARCHAR(64) NOT NULL,
  room_version BIGINT NOT NULL,
  payload LONGBLOB NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, room_id),
  INDEX idx_room_snapshots_version (app_code, room_id, room_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS room_command_log (
  app_code VARCHAR(32) NOT NULL,
  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,
  owner_node_id VARCHAR(128) NOT NULL DEFAULT '',
  lease_token VARCHAR(128) NOT NULL DEFAULT '',
  payload LONGBLOB NOT NULL,
  replayable BOOLEAN NOT NULL,
  created_at_ms BIGINT NOT NULL,
  UNIQUE KEY uk_room_command (app_code, room_id, command_id),
  KEY idx_room_command_replay (app_code, room_id, room_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS room_outbox (
  app_code VARCHAR(32) NOT NULL,
  event_id VARCHAR(128) NOT NULL,
  event_type VARCHAR(64) NOT NULL,
  room_id VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL,
  worker_id VARCHAR(128) NOT NULL DEFAULT '',
  lock_until_ms BIGINT NULL,
  envelope LONGBLOB NOT NULL,
  created_at_ms BIGINT NOT NULL,
  retry_count INT NOT NULL DEFAULT 0,
  last_error TEXT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, event_id),
  KEY idx_room_outbox_pending (app_code, status, created_at_ms),
  KEY idx_room_outbox_claim (app_code, status, lock_until_ms, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
