import { NextResponse } from "next/server"; import { prisma } from "@/lib/db"; import { withApiError } from "@/lib/api-error"; /** * GET /api/stats/personal-bests * * Returns all-time personal best solve times for Logic Puzzle categories, * grouped by exerciseType and difficulty. * * Only counts correct solves (score > 0) with a recorded durationSecs. * * Response shape: * { * records: Array<{ * exerciseType: string; * difficulty: "easy" | "medium" | "hard"; * bestSecs: number; * solveCount: number; // total correct solves for this type+difficulty * achievedAt: string; // ISO timestamp of the fastest solve * }>; * } */ const LOGIC_EXERCISE_TYPES = [ "sudoku", "sequence", "pattern", "wordscramble", "constraint", "mathequation", ] as const; const DIFFICULTIES = ["easy", "medium", "hard"] as const; const EXERCISE_LABELS: Record = { sudoku: "Sudoku", sequence: "Number Sequence", pattern: "Pattern Matrix", wordscramble: "Word Scramble", constraint: "Constraint Grid", mathequation: "Math Equation", }; export type PersonalBestRecord = { exerciseType: string; exerciseLabel: string; difficulty: "easy" | "medium" | "hard"; bestSecs: number; solveCount: number; achievedAt: string; }; export type PersonalBestsResponse = { records: PersonalBestRecord[]; }; export const GET = withApiError(async () => { // Fetch all correct logic puzzle activities with a duration in one query, // then process in-memory. Total row count is bounded (6 types × 3 diff × N // solves) so this is fine for a single-user app. const activities = await prisma.activity.findMany({ where: { userId: 1, exerciseType: { in: [...LOGIC_EXERCISE_TYPES] }, durationSecs: { not: null }, score: { gt: 0 }, }, select: { exerciseType: true, description: true, durationSecs: true, createdAt: true, }, orderBy: { durationSecs: "asc" }, }); // Group by exerciseType + difficulty. // Difficulty is embedded in the activity description as the word // "easy", "medium", or "hard" — consistent with how submit route sets it. const bestMap = new Map< string, // key: `${exerciseType}:${difficulty}` { bestSecs: number; solveCount: number; achievedAt: Date } >(); for (const act of activities) { // Determine difficulty from description let difficulty: "easy" | "medium" | "hard" | null = null; const desc = act.description.toLowerCase(); if (desc.includes("easy")) difficulty = "easy"; else if (desc.includes("medium")) difficulty = "medium"; else if (desc.includes("hard")) difficulty = "hard"; if (!difficulty) continue; const key = `${act.exerciseType}:${difficulty}`; const existing = bestMap.get(key); if (!existing) { // First entry — activities are ordered by durationSecs asc, so this // is guaranteed to be the fastest for this key. bestMap.set(key, { bestSecs: act.durationSecs!, solveCount: 1, achievedAt: act.createdAt, }); } else { // Accumulate solve count; bestSecs is already the minimum (first entry). existing.solveCount += 1; } } // Build ordered response: exercise types in display order, then easy→medium→hard. const records: PersonalBestRecord[] = []; for (const exerciseType of LOGIC_EXERCISE_TYPES) { for (const difficulty of DIFFICULTIES) { const key = `${exerciseType}:${difficulty}`; const entry = bestMap.get(key); if (!entry) continue; records.push({ exerciseType, exerciseLabel: EXERCISE_LABELS[exerciseType] ?? exerciseType, difficulty, bestSecs: entry.bestSecs, solveCount: entry.solveCount, achievedAt: entry.achievedAt.toISOString(), }); } } return NextResponse.json({ records } satisfies PersonalBestsResponse); });