USE aihub;

CREATE TABLE IF NOT EXISTS crash_audit_log (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  round_id INT NULL,
  user_id INT NULL,
  action VARCHAR(40) NOT NULL,
  detail JSON NULL,
  ip_address VARCHAR(45) NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_crash_audit_round (round_id),
  INDEX idx_crash_audit_user (user_id)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS crash_user_auto (
  user_id INT NOT NULL,
  slot TINYINT NOT NULL,
  stake DECIMAL(15,2) NOT NULL DEFAULT 1000,
  auto_cashout_target DECIMAL(10,2) NULL,
  auto_bet_enabled TINYINT(1) NOT NULL DEFAULT 0,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (user_id, slot),
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB;

ALTER TABLE crash_rounds
  ADD COLUMN server_seed_hash_pre VARCHAR(64) NULL AFTER betting_ends_at;

INSERT IGNORE INTO settings (setting_key, setting_value) VALUES
('crash_min_bet', '100'),
('crash_max_bet', '100000'),
('crash_payout_cap', '500000'),
('crash_sound_enabled', '1');
