/**
 * Daily challenge: a fixed-per-day mix of questions across all subjects.
 * Deterministic by date so every child sees the same set that day. Server-only.
 */
import type { Question } from './types';
import { SUBJECTS } from './types';
import { getLessonsForSubject } from './content';

export const DAILY_REWARD = 25;

function seededShuffle<T>(arr: T[], seed: number): T[] {
  const a = [...arr];
  let s = seed;
  for (let i = a.length - 1; i > 0; i -= 1) {
    s = (s * 9301 + 49297) % 233280;
    const j = Math.floor((s / 233280) * (i + 1));
    [a[i], a[j]] = [a[j], a[i]];
  }
  return a;
}

function dateSeed(): number {
  const d = new Date();
  return d.getFullYear() * 10000 + (d.getMonth() + 1) * 100 + d.getDate();
}

export function getDailyQuestions(count = 5): Question[] {
  const pool: Question[] = SUBJECTS.flatMap((s) =>
    getLessonsForSubject(s.id).flatMap((l) => l.questions),
  );
  if (pool.length === 0) return [];
  return seededShuffle(pool, dateSeed()).slice(0, Math.min(count, pool.length));
}
