-- =============================================================
-- 伙伴泡泡项目数据库初始化脚本
-- 数据库：MySQL 5.7+
-- 生成时间：2026-03-17
-- =============================================================

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

USE `market_bi`;

-- -------------------------------------------------------------
-- 1. 房间表 Room
--    每个玩家扫码进入的 6 位数字房间
-- -------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `Room` (
  `id`        INT          NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `roomId`    VARCHAR(6)   NOT NULL                COMMENT '6 位数字房间号（唯一）',
  `status`    ENUM('waiting','playing','finished')
              NOT NULL DEFAULT 'waiting'            COMMENT '房间状态',
  `createdAt` DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间',
  `updatedAt` DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
              ON UPDATE CURRENT_TIMESTAMP(3)        COMMENT '最后更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `Room_roomId_key` (`roomId`)
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4
  COLLATE=utf8mb4_unicode_ci
  COMMENT='房间表';


-- -------------------------------------------------------------
-- 2. 游戏局表 GameSession
--    一个房间可对应多局游戏，记录每局得分与时长
-- -------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `GameSession` (
  `id`        INT          NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `roomId`    VARCHAR(6)   NOT NULL                COMMENT '所属房间号（外键 → Room.roomId）',
  `score`     INT          NOT NULL DEFAULT 0      COMMENT '本局最终得分',
  `duration`  INT          NULL                    COMMENT '游戏时长（秒），结束后写入',
  `startedAt` DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '游戏开始时间',
  `endedAt`   DATETIME(3)  NULL                    COMMENT '游戏结束时间，进行中为 NULL',
  `status`    ENUM('playing','finished')
              NOT NULL DEFAULT 'playing'            COMMENT '游戏局状态',
  PRIMARY KEY (`id`),
  KEY `GameSession_roomId_idx` (`roomId`),
  KEY `GameSession_startedAt_idx` (`startedAt`),
  KEY `GameSession_score_idx` (`score`),
  CONSTRAINT `GameSession_roomId_fkey`
    FOREIGN KEY (`roomId`) REFERENCES `Room` (`roomId`)
    ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4
  COLLATE=utf8mb4_unicode_ci
  COMMENT='游戏局表';


-- -------------------------------------------------------------
-- 3. 大屏配置表 ScreenConfig
--    记录每块大屏的标识及当前绑定的房间
-- -------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `ScreenConfig` (
  `id`            INT          NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `screenName`    VARCHAR(64)  NOT NULL               COMMENT '大屏唯一标识名',
  `currentRoomId` VARCHAR(6)   NULL                   COMMENT '当前投屏的房间号，NULL 表示未绑定',
  `createdAt`     DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间',
  `updatedAt`     DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
                  ON UPDATE CURRENT_TIMESTAMP(3)      COMMENT '最后更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `ScreenConfig_screenName_key` (`screenName`)
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4
  COLLATE=utf8mb4_unicode_ci
  COMMENT='大屏配置表';
