import { NextRequest, NextResponse } from "next/server"; import { getRandomFlawPassage, getRandomSteelmanPosition, getRandomDevilsAdvocatePosition, type ExerciseType, type FlawDifficulty, } from "@/lib/critical/passages"; import { withApiError } from "@/lib/api-error"; /** * GET /api/critical/generate * * Query params: * type?: "flaw-finder" | "steelman" | "devils-advocate" * difficulty?: "easy" | "medium" | "hard" (only for flaw-finder) */ export const GET = withApiError(async (request: NextRequest) => { const { searchParams } = new URL(request.url); const rawType = searchParams.get("type"); const rawDiff = searchParams.get("difficulty"); const validTypes: ExerciseType[] = ["flaw-finder", "steelman", "devils-advocate"]; const validDiffs: FlawDifficulty[] = ["easy", "medium", "hard"]; const type: ExerciseType = rawType && validTypes.includes(rawType as ExerciseType) ? (rawType as ExerciseType) : validTypes[Math.floor(Math.random() * validTypes.length)]; const difficulty: FlawDifficulty | undefined = rawDiff && validDiffs.includes(rawDiff as FlawDifficulty) ? (rawDiff as FlawDifficulty) : undefined; if (type === "flaw-finder") { const passage = getRandomFlawPassage(difficulty); return NextResponse.json({ type, passage: { id: passage.id, difficulty: passage.difficulty, title: passage.title, passage: passage.passage, fallacyCount: passage.fallacies.length, // do NOT return fallacies here — those are the answer key }, }); } if (type === "steelman") { const position = getRandomSteelmanPosition(); return NextResponse.json({ type, position: { id: position.id, domain: position.domain, position: position.position, context: position.context, }, }); } // devils-advocate const position = getRandomDevilsAdvocatePosition(); return NextResponse.json({ type, position: { id: position.id, domain: position.domain, position: position.position, context: position.context, }, }); });