import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '../../../lib/prisma';

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();

    if (!body.questionId || !body.selectedAnswer) {
      return NextResponse.json(
        { error: 'اطلاعات ناقص است' },
        { status: 400 }
      );
    }

    const question = await prisma.question.findUnique({
      where: { id: body.questionId },
    });

    if (!question) {
      return NextResponse.json(
        { error: 'سوال یافت نشد' },
        { status: 404 }
      );
    }

    const correct = question.correctAnswer === body.selectedAnswer;

    return NextResponse.json({
      correct,
      correctAnswer: question.correctAnswer,
    });
  } catch (error) {
    console.error('Error checking answer:', error);
    return NextResponse.json(
      { error: 'خطا در بررسی پاسخ' },
      { status: 500 }
    );
  }
}
