CREATE TABLE IF NOT EXISTS voice_room_rocket_config (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL COMMENT '系统来源',
  enabled TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  timezone VARCHAR(64) NOT NULL DEFAULT 'Asia/Riyadh' COMMENT '业务时区',
  max_level INT NOT NULL DEFAULT 6 COMMENT '最大等级',
  ranking_top_limit INT NOT NULL DEFAULT 100 COMMENT '火箭王榜单展示人数',
  reward_record_keep_days INT NOT NULL DEFAULT 35 COMMENT '用户奖励记录展示天数',
  broadcast_duration_seconds INT NOT NULL DEFAULT 7 COMMENT '发射广播展示秒数',
  in_room_reward_delay_seconds INT NOT NULL DEFAULT 10 COMMENT '在房间奖励延迟窗口秒数',
  energy_rule_json JSON DEFAULT NULL COMMENT '礼物折算规则',
  rule_text JSON DEFAULT NULL COMMENT '规则说明多语言文案',
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_voice_room_rocket_config (sys_origin)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音房火箭系统配置';

CREATE TABLE IF NOT EXISTS voice_room_rocket_level_config (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL,
  level INT NOT NULL COMMENT 'Lv.1-Lv.6',
  need_energy BIGINT NOT NULL COMMENT '本级发射所需能量',
  shake_threshold_percent DECIMAL(8,4) NOT NULL DEFAULT 0 COMMENT '抖动阈值百分比',
  rocket_icon_url VARCHAR(512) DEFAULT NULL,
  rocket_animation_url VARCHAR(512) DEFAULT NULL,
  progress_bar_url VARCHAR(512) DEFAULT NULL,
  enabled TINYINT(1) NOT NULL DEFAULT 1,
  sort INT NOT NULL DEFAULT 0,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_voice_room_rocket_level (sys_origin, level)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音房火箭等级配置';

CREATE TABLE IF NOT EXISTS voice_room_rocket_reward_config (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL,
  level INT NOT NULL COMMENT '适用等级',
  reward_scene VARCHAR(32) NOT NULL COMMENT 'TOP1/IGNITE/IN_ROOM',
  reward_type VARCHAR(32) NOT NULL COMMENT 'GOLD/MOUNT/AVATAR_FRAME/CHAT_BUBBLE/VIP_CARD/GIFT/BADGE',
  reward_item_id BIGINT DEFAULT NULL COMMENT '道具、礼物、权益等资源ID',
  reward_name VARCHAR(128) NOT NULL,
  reward_cover VARCHAR(512) DEFAULT NULL,
  reward_amount BIGINT NOT NULL DEFAULT 1 COMMENT '金币数量或道具数量/天数',
  expire_days INT DEFAULT NULL COMMENT '时效类奖励有效天数',
  weight INT NOT NULL DEFAULT 0 COMMENT 'IN_ROOM 随机权重；0 表示固定发放',
  sort INT NOT NULL DEFAULT 0,
  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,
  KEY idx_voice_room_rocket_reward_scene (sys_origin, level, reward_scene, enabled)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音房火箭奖励配置';

CREATE TABLE IF NOT EXISTS voice_room_rocket_status (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL,
  room_id BIGINT NOT NULL,
  day_key VARCHAR(16) NOT NULL COMMENT 'UTC+3 业务日 yyyyMMdd',
  round_no INT NOT NULL DEFAULT 1,
  current_level INT NOT NULL DEFAULT 1,
  current_energy BIGINT NOT NULL DEFAULT 0,
  need_energy BIGINT NOT NULL,
  status VARCHAR(32) NOT NULL COMMENT 'CHARGING/LAUNCHING',
  level_start_time DATETIME NOT NULL,
  last_event_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_voice_room_rocket_status (sys_origin, room_id, day_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音房火箭当前状态';

CREATE TABLE IF NOT EXISTS voice_room_rocket_level_round (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL,
  room_id BIGINT NOT NULL,
  day_key VARCHAR(16) NOT NULL,
  round_no INT NOT NULL,
  level INT NOT NULL,
  need_energy BIGINT NOT NULL,
  final_energy BIGINT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL COMMENT 'CHARGING/LAUNCHED',
  top1_user_id BIGINT DEFAULT NULL,
  ignite_user_id BIGINT DEFAULT NULL,
  reach_time DATETIME DEFAULT NULL COMMENT '能量达标时间',
  launch_time DATETIME DEFAULT NULL COMMENT '发射时间',
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_voice_room_rocket_level_round (sys_origin, room_id, day_key, round_no, level),
  KEY idx_voice_room_rocket_level_round_room (sys_origin, room_id, day_key, round_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音房火箭每轮每级快照';

CREATE TABLE IF NOT EXISTS voice_room_rocket_contribution (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL,
  room_id BIGINT NOT NULL,
  day_key VARCHAR(16) NOT NULL,
  round_no INT NOT NULL,
  level INT NOT NULL,
  user_id BIGINT NOT NULL,
  score_energy BIGINT NOT NULL DEFAULT 0,
  gift_count INT NOT NULL DEFAULT 0,
  first_contribute_time DATETIME NOT NULL,
  last_contribute_time DATETIME NOT NULL,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_voice_room_rocket_contribution_user (sys_origin, room_id, day_key, round_no, level, user_id),
  KEY idx_voice_room_rocket_contribution_rank (sys_origin, room_id, day_key, round_no, level, score_energy, last_contribute_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音房火箭贡献榜';

CREATE TABLE IF NOT EXISTS voice_room_rocket_gift_event_log (
  id BIGINT NOT NULL PRIMARY KEY,
  sys_origin VARCHAR(32) NOT NULL,
  track_id VARCHAR(128) NOT NULL,
  room_id BIGINT NOT NULL,
  send_user_id BIGINT NOT NULL,
  gift_id BIGINT DEFAULT NULL,
  gift_type VARCHAR(32) DEFAULT NULL,
  gift_tab VARCHAR(64) DEFAULT NULL,
  raw_energy BIGINT NOT NULL DEFAULT 0,
  accepted_energy BIGINT NOT NULL DEFAULT 0,
  day_key VARCHAR(16) NOT NULL,
  round_no INT NOT NULL,
  level INT NOT NULL,
  status VARCHAR(32) NOT NULL COMMENT 'IGNORED/SUCCESS/FAILED',
  raw_payload MEDIUMTEXT,
  error_message VARCHAR(1024) DEFAULT NULL,
  event_time DATETIME NOT NULL,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_voice_room_rocket_track (track_id),
  KEY idx_voice_room_rocket_event_room (sys_origin, room_id, day_key, round_no, level)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音房火箭送礼事件日志';

CREATE TABLE IF NOT EXISTS voice_room_rocket_launch (
  id BIGINT NOT NULL PRIMARY KEY,
  launch_no VARCHAR(64) NOT NULL,
  sys_origin VARCHAR(32) NOT NULL,
  room_id BIGINT NOT NULL,
  room_account VARCHAR(64) DEFAULT NULL,
  day_key VARCHAR(16) NOT NULL,
  round_no INT NOT NULL,
  level INT NOT NULL,
  final_energy BIGINT NOT NULL,
  top1_user_id BIGINT DEFAULT NULL,
  ignite_user_id BIGINT DEFAULT NULL,
  in_room_snapshot_status VARCHAR(32) NOT NULL DEFAULT 'PENDING',
  launch_time DATETIME NOT NULL,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_voice_room_rocket_launch_no (launch_no),
  UNIQUE KEY uk_voice_room_rocket_launch_level (sys_origin, room_id, day_key, round_no, level),
  KEY idx_voice_room_rocket_launch_room (sys_origin, room_id, launch_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音房火箭发射记录';

CREATE TABLE IF NOT EXISTS voice_room_rocket_reward_record (
  id BIGINT NOT NULL PRIMARY KEY,
  launch_id BIGINT NOT NULL,
  launch_no VARCHAR(64) NOT NULL,
  sys_origin VARCHAR(32) NOT NULL,
  room_id BIGINT NOT NULL,
  day_key VARCHAR(16) NOT NULL,
  round_no INT NOT NULL,
  level INT NOT NULL,
  user_id BIGINT NOT NULL,
  reward_scene VARCHAR(32) NOT NULL,
  reward_config_id BIGINT NOT NULL,
  reward_type VARCHAR(32) NOT NULL,
  reward_item_id BIGINT DEFAULT NULL,
  reward_name VARCHAR(128) NOT NULL,
  reward_cover VARCHAR(512) DEFAULT NULL,
  reward_amount BIGINT NOT NULL DEFAULT 1,
  expire_days INT DEFAULT NULL,
  grant_event_id VARCHAR(128) NOT NULL,
  grant_status VARCHAR(32) NOT NULL COMMENT 'PENDING/SUCCESS/FAILED',
  popup_status VARCHAR(32) NOT NULL DEFAULT 'UNREAD' COMMENT 'UNREAD/READ',
  error_message VARCHAR(1024) DEFAULT NULL,
  grant_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_voice_room_rocket_reward_event (grant_event_id),
  KEY idx_voice_room_rocket_reward_user (sys_origin, user_id, create_time),
  KEY idx_voice_room_rocket_reward_launch (launch_id, user_id),
  KEY idx_voice_room_rocket_reward_retry (grant_status, update_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音房火箭奖励记录';

CREATE TABLE IF NOT EXISTS voice_room_rocket_audience_snapshot (
  id BIGINT NOT NULL PRIMARY KEY,
  launch_id BIGINT NOT NULL,
  sys_origin VARCHAR(32) NOT NULL,
  room_id BIGINT NOT NULL,
  user_id BIGINT NOT NULL,
  last_seen_time DATETIME NOT NULL,
  selected TINYINT(1) NOT NULL DEFAULT 0,
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uk_voice_room_rocket_audience (launch_id, user_id),
  KEY idx_voice_room_rocket_audience_launch (launch_id, selected)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音房火箭在房间用户快照';
