-- 游戏资格系统表
-- Migration: 006_game_tickets.sql

-- 用户游戏资格
CREATE TABLE IF NOT EXISTS user_game_tickets (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  created_at DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  user_id BIGINT NOT NULL COMMENT '用户ID',
  game_code VARCHAR(32) NOT NULL DEFAULT 'minesweeper' COMMENT '游戏代码',
  available INT NOT NULL DEFAULT 0 COMMENT '可用次数',
  total_earned INT NOT NULL DEFAULT 0 COMMENT '累计获得',
  total_used INT NOT NULL DEFAULT 0 COMMENT '累计使用',
  UNIQUE KEY uk_user_game (user_id, game_code),
  INDEX idx_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户游戏资格';

-- 资格变动日志
CREATE TABLE IF NOT EXISTS game_ticket_logs (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  created_at DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3),
  user_id BIGINT NOT NULL COMMENT '用户ID',
  game_code VARCHAR(32) NOT NULL COMMENT '游戏代码',
  change_type TINYINT NOT NULL COMMENT '1=获得 2=使用',
  amount INT NOT NULL COMMENT '变动数量',
  balance INT NOT NULL COMMENT '变动后余额',
  source VARCHAR(32) COMMENT '来源: order/task/admin',
  source_id BIGINT COMMENT '来源ID',
  remark VARCHAR(255) COMMENT '备注',
  INDEX idx_user_game (user_id, game_code),
  INDEX idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='游戏资格变动日志';
