import { NextRequest, NextResponse } from 'next/server'; import { GeneratedQuestion } from '@/types/quiz'; export async function PUT(req: NextRequest) { try { const updatedQuestion: GeneratedQuestion = await req.json(); // Validate the question structure if (!updatedQuestion.id || !updatedQuestion.stem || !updatedQuestion.content) { return NextResponse.json( { error: 'Invalid question data' }, { status: 400 } ); } // Validate required fields const { Question, Options, Answer } = updatedQuestion.content; if (!Question || !Options || !Answer) { return NextResponse.json( { error: 'Missing required question content' }, { status: 400 } ); } // Validate options structure const requiredOptions = ['A', 'B', 'C', 'D']; for (const option of requiredOptions) { if (!Options[option as keyof typeof Options]) { return NextResponse.json( { error: `Missing option ${option}` }, { status: 400 } ); } } // Validate answer is one of the options if (!requiredOptions.includes(Answer)) { return NextResponse.json( { error: 'Invalid answer option' }, { status: 400 } ); } // Validate points is a positive number if (typeof updatedQuestion.points !== 'number' || updatedQuestion.points < 1) { return NextResponse.json( { error: 'Points must be a positive number' }, { status: 400 } ); } // In a real application, you would save this to a database // For now, we'll just return the validated question // Note: We preserve the original type and points as they shouldn't be modified const validatedQuestion: GeneratedQuestion = { ...updatedQuestion, // Preserve original type and points (these should not be modified) type: updatedQuestion.type, // Keep original type points: updatedQuestion.points, // Keep original points // Ensure the content.Question matches the stem for consistency content: { ...updatedQuestion.content, Question: updatedQuestion.stem, }, // Update the timestamp to reflect the edit createdAt: new Date(), }; return NextResponse.json(validatedQuestion); } catch (error) { console.error('Error updating question:', error); return NextResponse.json( { error: 'Failed to update question' }, { status: 500 } ); } }