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

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

    if (!body.questionIds || !Array.isArray(body.questionIds)) {
      return NextResponse.json(
        { error: 'لیست سوالات نامعتبر است' },
        { status: 400 }
      );
    }

    const updates = body.questionIds.map((id: string, index: number) =>
      prisma.question.update({
        where: { id },
        data: { order: index },
      })
    );

    await Promise.all(updates);

    return NextResponse.json({ success: true });
  } catch (error) {
    console.error('Error reordering questions:', error);
    return NextResponse.json(
      { error: 'خطا در تغییر ترتیب سوالات' },
      { status: 500 }
    );
  }
}
