USE aihub;

UPDATE games SET config_json = '{"sides":["red","green","blue","yellow","purple","orange"],"multipliers":{"red":1.96,"green":1.96,"blue":2.8,"yellow":2.2,"purple":2.5,"orange":2.3}}'
WHERE game_code = 'color_pick';

INSERT IGNORE INTO games (game_code, name, game_type, icon, config_json, min_bet, max_bet, win_multiplier, max_payout_per_round, sort_order) VALUES
('luck_numbers', 'Luck Numbers', 'luck_numbers', '🎯', '{"poolMax":100,"pickCount":5,"roundSeconds":60,"positionMultipliers":[1.5,2,3,5,10],"fullMatchMultiplier":50}', 500, 100000, 50, 500000, 7);

INSERT IGNORE INTO settings (setting_key, setting_value) VALUES
('luck_round_seconds', '60'),
('luck_draw_pause_seconds', '3');

CREATE TABLE IF NOT EXISTS luck_number_rounds (
  id INT AUTO_INCREMENT PRIMARY KEY,
  phase ENUM('betting','drawing','complete') NOT NULL DEFAULT 'betting',
  winning_numbers JSON NULL,
  betting_ends_at DATETIME NOT NULL,
  drawn_at DATETIME NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_phase (phase)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS luck_number_bets (
  id INT AUTO_INCREMENT PRIMARY KEY,
  round_id INT NOT NULL,
  user_id INT NOT NULL,
  game_id INT NOT NULL,
  stake DECIMAL(15,2) NOT NULL,
  numbers JSON NOT NULL,
  payout DECIMAL(15,2) NOT NULL DEFAULT 0,
  matches INT NOT NULL DEFAULT 0,
  status ENUM('pending','won','lost') NOT NULL DEFAULT 'pending',
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (round_id) REFERENCES luck_number_rounds(id) ON DELETE CASCADE,
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  FOREIGN KEY (game_id) REFERENCES games(id) ON DELETE RESTRICT,
  UNIQUE KEY uq_round_user (round_id, user_id),
  INDEX idx_user (user_id)
) ENGINE=InnoDB;
