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

export async function PUT(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const body = await request.json();

    const question = await prisma.question.update({
      where: { id: params.id },
      data: {
        text: body.text?.trim(),
        options: body.options ? JSON.stringify(body.options) : undefined,
        correctAnswer: body.correctAnswer,
        order: body.order,
      },
    });

    return NextResponse.json({ question });
  } catch (error) {
    console.error('Error updating question:', error);
    return NextResponse.json(
      { error: 'خطا در ویرایش سوال' },
      { status: 500 }
    );
  }
}

export async function DELETE(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    await prisma.question.delete({
      where: { id: params.id },
    });

    return NextResponse.json({ success: true });
  } catch (error) {
    console.error('Error deleting question:', error);
    return NextResponse.json(
      { error: 'خطا در حذف سوال' },
      { status: 500 }
    );
  }
}
