-- 扫雷游戏：每局每人的对战记录
CREATE TABLE IF NOT EXISTS `minesweeper_game_records` (
  `id`               BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `match_id`         VARCHAR(128)    NOT NULL DEFAULT '' COMMENT 'Nakama match ID',
  `user_id`          BIGINT          NOT NULL COMMENT '用户ID',
  `game_type`        VARCHAR(32)     NOT NULL DEFAULT 'minesweeper' COMMENT 'minesweeper / minesweeper_free',
  `ticket`           VARCHAR(64)     NOT NULL DEFAULT '' COMMENT '入场券',
  `is_winner`        TINYINT(1)      NOT NULL DEFAULT 0,
  `rank_position`    TINYINT         NOT NULL DEFAULT 0 COMMENT '本局名次',
  `total_players`    TINYINT         NOT NULL DEFAULT 0,
  `total_rounds`     INT             NOT NULL DEFAULT 0 COMMENT '游戏总轮次',
  `rounds_survived`  INT             NOT NULL DEFAULT 0 COMMENT '存活轮次',
  `score`            INT             NOT NULL DEFAULT 0,
  `damage_dealt`     INT             NOT NULL DEFAULT 0,
  `damage_taken`     INT             NOT NULL DEFAULT 0,
  `kills`            INT             NOT NULL DEFAULT 0,
  `chests_collected` INT             NOT NULL DEFAULT 0,
  `rank_points`      INT             NOT NULL DEFAULT 0 COMMENT '积分变动',
  `raw_summary`      JSON                         COMMENT '完整结算数据快照',
  `settled_at`       DATETIME        NOT NULL,
  `created_at`       DATETIME        NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_match_user` (`match_id`, `user_id`),
  KEY `idx_user_game` (`user_id`, `game_type`),
  KEY `idx_settled_at` (`settled_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='扫雷对战记录';

-- 扫雷游戏：排行榜（每人每游戏类型一行，累计聚合）
CREATE TABLE IF NOT EXISTS `minesweeper_leaderboard` (
  `id`                    BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`               BIGINT          NOT NULL,
  `game_type`             VARCHAR(32)     NOT NULL DEFAULT 'minesweeper',
  `matches_played`        INT             NOT NULL DEFAULT 0,
  `wins`                  INT             NOT NULL DEFAULT 0,
  `losses`                INT             NOT NULL DEFAULT 0,
  `win_rate`              DECIMAL(6,4)    NOT NULL DEFAULT 0.0000,
  `total_score`           INT             NOT NULL DEFAULT 0,
  `best_score`            INT             NOT NULL DEFAULT 0,
  `avg_score`             DECIMAL(10,2)   NOT NULL DEFAULT 0.00,
  `total_damage_dealt`    INT             NOT NULL DEFAULT 0,
  `total_damage_taken`    INT             NOT NULL DEFAULT 0,
  `avg_damage_dealt`      DECIMAL(10,2)   NOT NULL DEFAULT 0.00,
  `total_chests_collected` INT            NOT NULL DEFAULT 0,
  `total_rounds_survived` INT             NOT NULL DEFAULT 0,
  `total_rank_points`     INT             NOT NULL DEFAULT 0,
  `last_match_id`         VARCHAR(128)    NOT NULL DEFAULT '',
  `last_settled_at`       DATETIME        NOT NULL,
  `created_at`            DATETIME        NOT NULL,
  `updated_at`            DATETIME        NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_game` (`user_id`, `game_type`),
  KEY `idx_rank_points` (`game_type`, `total_rank_points` DESC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='扫雷排行榜聚合';
