// lottery.jsx — runs after a kid passes a math game.
//
// Win odds: base 30%, plus up to 25% performance bonus, capped at 55%.
// We never reissue a code: pickUnwonLotteryCode skips codes the profile has
// already won. If everything is won, we still return won:false so the kid
// hears a friendly "almost!" rather than an empty reward.

const BASE_WIN_RATE = 0.30;
const MAX_PERF_BONUS = 0.25;
const MAX_WIN_RATE = 0.55;

// performance is a 0..1 score from adaptive.performanceScore.
// forceWin (dev-only) bypasses the random roll for tweaks-panel testing.
function runLottery(profile, performance, opts = {}) {
  const perf = typeof performance === 'number' ? performance : 0.5;
  const odds = Math.min(MAX_WIN_RATE, BASE_WIN_RATE + perf * MAX_PERF_BONUS);
  const roll = opts.forceWin ? 0 : Math.random();
  if (roll >= odds) return { won: false, entry: null, odds };
  const entry = pickUnwonLotteryCode(profile);
  if (!entry) return { won: false, entry: null, odds, exhausted: true };
  return { won: true, entry, odds };
}

Object.assign(window, { runLottery, BASE_WIN_RATE, MAX_PERF_BONUS, MAX_WIN_RATE });
