-- Bonus Rounds Table
CREATE TABLE IF NOT EXISTS bonus_rounds (
  id INT AUTO_INCREMENT PRIMARY KEY,
  userId INT NOT NULL,
  gameId INT NOT NULL,
  bonusType ENUM('pick_em', 'wheel_spin', 'free_spins', 'multiplier') NOT NULL,
  triggerSpinId VARCHAR(255) NOT NULL,
  betAmount DECIMAL(10, 2) NOT NULL,
  multiplier INT DEFAULT 1,
  freeSpinsCount INT DEFAULT 0,
  bonusWinAmount DECIMAL(10, 2) DEFAULT 0,
  status ENUM('active', 'completed', 'expired') DEFAULT 'active',
  createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  completedAt TIMESTAMP NULL,
  expiresAt TIMESTAMP NULL,
  FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE,
  INDEX idx_userId_status (userId, status),
  INDEX idx_gameId (gameId)
);

-- Bonus Game Choices (for pick-em style bonuses)
CREATE TABLE IF NOT EXISTS bonus_choices (
  id INT AUTO_INCREMENT PRIMARY KEY,
  bonusRoundId INT NOT NULL,
  choiceIndex INT NOT NULL,
  symbol VARCHAR(50) NOT NULL,
  value DECIMAL(10, 2) NOT NULL,
  isSelected BOOLEAN DEFAULT FALSE,
  selectedAt TIMESTAMP NULL,
  FOREIGN KEY (bonusRoundId) REFERENCES bonus_rounds(id) ON DELETE CASCADE,
  INDEX idx_bonusRoundId (bonusRoundId)
);

-- Progressive Jackpot Table
CREATE TABLE IF NOT EXISTS progressive_jackpots (
  id INT AUTO_INCREMENT PRIMARY KEY,
  gameId INT NOT NULL,
  currentAmount DECIMAL(15, 2) NOT NULL DEFAULT 10000,
  baseAmount DECIMAL(15, 2) NOT NULL DEFAULT 10000,
  contributionRate DECIMAL(5, 4) NOT NULL DEFAULT 0.01,
  lastWinnerUserId INT,
  lastWinAmount DECIMAL(15, 2),
  lastWinAt TIMESTAMP NULL,
  totalContributed DECIMAL(15, 2) DEFAULT 0,
  createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY unique_gameId (gameId),
  FOREIGN KEY (lastWinnerUserId) REFERENCES users(id) ON DELETE SET NULL,
  INDEX idx_gameId (gameId)
);

-- Jackpot Win History
CREATE TABLE IF NOT EXISTS jackpot_wins (
  id INT AUTO_INCREMENT PRIMARY KEY,
  jackpotId INT NOT NULL,
  userId INT NOT NULL,
  winAmount DECIMAL(15, 2) NOT NULL,
  spinId VARCHAR(255) NOT NULL,
  wonAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (jackpotId) REFERENCES progressive_jackpots(id) ON DELETE CASCADE,
  FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE,
  INDEX idx_userId (userId),
  INDEX idx_jackpotId (jackpotId),
  INDEX idx_wonAt (wonAt)
);

-- Tournament Mode Table
CREATE TABLE IF NOT EXISTS tournaments (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  description TEXT,
  gameId INT NOT NULL,
  status ENUM('upcoming', 'active', 'completed', 'cancelled') DEFAULT 'upcoming',
  entryFee DECIMAL(10, 2) NOT NULL,
  maxParticipants INT,
  currentParticipants INT DEFAULT 0,
  prizePool DECIMAL(15, 2) NOT NULL,
  startTime TIMESTAMP NOT NULL,
  endTime TIMESTAMP NOT NULL,
  createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_status (status),
  INDEX idx_gameId (gameId),
  INDEX idx_startTime (startTime)
);

-- Tournament Entries
CREATE TABLE IF NOT EXISTS tournament_entries (
  id INT AUTO_INCREMENT PRIMARY KEY,
  tournamentId INT NOT NULL,
  userId INT NOT NULL,
  score DECIMAL(15, 2) DEFAULT 0,
  spinsCount INT DEFAULT 0,
  totalWagered DECIMAL(15, 2) DEFAULT 0,
  totalWon DECIMAL(15, 2) DEFAULT 0,
  finalRank INT,
  prizeWon DECIMAL(15, 2) DEFAULT 0,
  enteredAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (tournamentId) REFERENCES tournaments(id) ON DELETE CASCADE,
  FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE,
  UNIQUE KEY unique_tournament_user (tournamentId, userId),
  INDEX idx_tournamentId (tournamentId),
  INDEX idx_userId (userId),
  INDEX idx_score (score DESC)
);

-- Tournament Prize Distribution
CREATE TABLE IF NOT EXISTS tournament_prizes (
  id INT AUTO_INCREMENT PRIMARY KEY,
  tournamentId INT NOT NULL,
  rank INT NOT NULL,
  prizeAmount DECIMAL(15, 2) NOT NULL,
  prizePercentage DECIMAL(5, 2),
  FOREIGN KEY (tournamentId) REFERENCES tournaments(id) ON DELETE CASCADE,
  UNIQUE KEY unique_tournament_rank (tournamentId, rank),
  INDEX idx_tournamentId (tournamentId)
);

-- Tournament Leaderboard Cache (for performance)
CREATE TABLE IF NOT EXISTS tournament_leaderboard (
  id INT AUTO_INCREMENT PRIMARY KEY,
  tournamentId INT NOT NULL,
  userId INT NOT NULL,
  rank INT NOT NULL,
  score DECIMAL(15, 2) NOT NULL,
  lastUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (tournamentId) REFERENCES tournaments(id) ON DELETE CASCADE,
  FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE,
  UNIQUE KEY unique_tournament_user_rank (tournamentId, userId),
  INDEX idx_tournamentId_rank (tournamentId, rank),
  INDEX idx_lastUpdated (lastUpdated)
);

-- Add columns to transactions table for bonus tracking
ALTER TABLE transactions ADD COLUMN IF NOT EXISTS bonusRoundId INT;
ALTER TABLE transactions ADD COLUMN IF NOT EXISTS tournamentId INT;
ALTER TABLE transactions ADD COLUMN IF NOT EXISTS jackpotId INT;
ALTER TABLE transactions ADD FOREIGN KEY (bonusRoundId) REFERENCES bonus_rounds(id) ON DELETE SET NULL;
ALTER TABLE transactions ADD FOREIGN KEY (tournamentId) REFERENCES tournaments(id) ON DELETE SET NULL;
ALTER TABLE transactions ADD FOREIGN KEY (jackpotId) REFERENCES progressive_jackpots(id) ON DELETE SET NULL;
