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

export async function PUT(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    // Need to await params to get the id
    const { id } = await params;
    const body = await request.json();
    const { status } = body;

    const updatedUserProduct = await prisma.userProduct.update({
      where: { id },
      data: { challengeStatus: status },
      include: { user: true, product: true }
    });

    return NextResponse.json(updatedUserProduct);
  } catch (error) {
    console.error('Error updating challenge status:', error);
    return NextResponse.json(
      { error: 'خطا در بروزرسانی وضعیت' },
      { status: 500 }
    );
  }
}