import type { PerformanceSummary, CategoryPerformance } from "./performance"; import type { Category } from "@/config/xp"; import { CATEGORIES } from "@/config/xp"; export type DifficultyLevel = "easy" | "medium" | "hard"; // --------------------------------------------------------------------------- // Thresholds // --------------------------------------------------------------------------- /** Success rate (0–100) below which we recommend easy difficulty */ const EASY_THRESHOLD = 40; /** Success rate (0–100) at or above which we recommend hard difficulty */ const HARD_THRESHOLD = 70; /** Streak days at or above which we boost difficulty one level */ const HOT_STREAK_DAYS = 5; // --------------------------------------------------------------------------- // Core selectors // --------------------------------------------------------------------------- /** * Select a base difficulty level from a success rate (0–100). * * < 40% → easy (user is struggling) * 40–69% → medium (building consistency) * ≥ 70% → hard (ready for a challenge) */ export function selectDifficulty(successRate: number): DifficultyLevel { if (successRate < EASY_THRESHOLD) return "easy"; if (successRate < HARD_THRESHOLD) return "medium"; return "hard"; } /** * Shift a difficulty one step up or down, clamped to valid values. */ function bumpDifficulty( level: DifficultyLevel, direction: "up" | "down", ): DifficultyLevel { const order: DifficultyLevel[] = ["easy", "medium", "hard"]; const idx = order.indexOf(level); const next = direction === "up" ? idx + 1 : idx - 1; return order[Math.max(0, Math.min(2, next))]; } // --------------------------------------------------------------------------- // Next-skill labels per category and level band // --------------------------------------------------------------------------- const NEXT_SKILL_MAP: Record = { chess: [ "basic tactics", "pattern recognition", "complex combinations", ], reading: [ "active recall", "inference & analysis", "critical evaluation", ], feynman: [ "concept fundamentals", "simplified explanation", "teaching mastery", ], logic: [ "basic deduction", "multi-step reasoning", "complex problem solving", ], "critical-thinking": [ "identifying fallacies", "argument reconstruction", "steelman & synthesis", ], quests: [ "foundational concepts", "applied knowledge", "expert application", ], coding: [ "core implementation", "debugging & review", "system architecture", ], }; function getNextSkill(category: Category, level: number): string { const [beginner, intermediate, advanced] = NEXT_SKILL_MAP[category]; if (level <= 2) return beginner; if (level <= 5) return intermediate; return advanced; } // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- export interface DifficultyRecommendation { difficulty: DifficultyLevel; nextSkill: string; adjustmentReason: string; /** False when the category has no activity history (brand-new user). */ hasHistory: boolean; } export interface DifficultyRecommendations { recommendations: Record; updatedAt: string; } // --------------------------------------------------------------------------- // Main recommendation builder // --------------------------------------------------------------------------- /** * Compute per-category difficulty recommendations from a performance summary. * * Adjustment rules (applied in order): * 1. Base difficulty from success rate bands. * 2. Hot streak (≥ 5 days): bump up one level. * 3. Cold streak (0 days, but user has some history): bump down one level. * 4. Improving trend: bump up one level (capped at hard). * 5. Declining trend: bump down one level (capped at easy). * * Edge cases: * - Category with 0 attempts: returns medium with hasHistory = false. * - Brand-new user (totalExercises = 0): all categories get hasHistory = false. */ export function getRecommendedSettings( summary: PerformanceSummary, ): DifficultyRecommendations { const recommendations = {} as Record; for (const cat of summary.categories as CategoryPerformance[]) { const category = cat.category; // No history for this category yet if (cat.attempts === 0) { recommendations[category] = { difficulty: "medium", nextSkill: getNextSkill(category, cat.level), adjustmentReason: "No activity yet — starting at medium difficulty", hasHistory: false, }; continue; } // --- Step 1: base difficulty from success rate --- let adjusted = selectDifficulty(cat.successRate); const reasons: string[] = [buildBaseReason(adjusted, cat.successRate)]; // --- Step 2: hot streak boost --- if (summary.streakDays >= HOT_STREAK_DAYS) { const bumped = bumpDifficulty(adjusted, "up"); if (bumped !== adjusted) { adjusted = bumped; reasons.push(`hot streak (${summary.streakDays} days) boosted difficulty`); } } // --- Step 3: cold/broken streak reduction --- if (summary.streakDays === 0 && summary.totalExercises > 0) { const bumped = bumpDifficulty(adjusted, "down"); if (bumped !== adjusted) { adjusted = bumped; reasons.push("easing back in after a break"); } } // --- Step 4 & 5: trend adjustment --- if (cat.trend === "improving") { const bumped = bumpDifficulty(adjusted, "up"); if (bumped !== adjusted) { adjusted = bumped; reasons.push("upward trend detected"); } } else if (cat.trend === "declining") { const bumped = bumpDifficulty(adjusted, "down"); if (bumped !== adjusted) { adjusted = bumped; reasons.push("declining trend detected"); } } recommendations[category] = { difficulty: adjusted, nextSkill: getNextSkill(category, cat.level), adjustmentReason: reasons.join(" — "), hasHistory: true, }; } // Ensure all categories are present even if missing from summary.categories for (const cat of CATEGORIES) { if (!recommendations[cat]) { recommendations[cat] = { difficulty: "medium", nextSkill: getNextSkill(cat, 1), adjustmentReason: "No activity yet — starting at medium difficulty", hasHistory: false, }; } } return { recommendations, updatedAt: new Date().toISOString(), }; } // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- function buildBaseReason( difficulty: DifficultyLevel, successRate: number, ): string { const pct = successRate.toFixed(1); switch (difficulty) { case "easy": return `Low success rate (${pct}%) — focusing on fundamentals`; case "medium": return `Moderate success rate (${pct}%) — building consistency`; case "hard": return `High success rate (${pct}%) — ready for a challenge`; } }