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

export async function POST(
  req: Request,
  { params }: { params: { id: string } }
) {
  try {
    const {
      designJson,
      designPreviewUrl,
      designPreviewUrlBack,
      placementPreviewUrl,
      placementPreviewUrlBack,
      designWidthCm,
      designHeightCm,
    } = await req.json();

    const design = await prisma.designApproval.findUnique({
      where: { id: params.id },
    });

    if (!design) {
      return NextResponse.json({ error: 'Design not found' }, { status: 404 });
    }

    const updated = await prisma.designApproval.update({
      where: { id: params.id },
      data: {
        ...(designJson && { designJson }),
        ...(designPreviewUrl && { designPreviewUrl }),
        ...(designPreviewUrlBack && { designPreviewUrlBack }),
        ...(placementPreviewUrl && { placementPreviewUrl }),
        ...(placementPreviewUrlBack && { placementPreviewUrlBack }),
        ...(designWidthCm !== undefined && { designWidthCm }),
        ...(designHeightCm !== undefined && { designHeightCm }),
      },
    });

    return NextResponse.json(updated);
  } catch (err) {
    console.error('Error modifying design:', err);
    return NextResponse.json(
      { error: 'Failed to modify design' },
      { status: 500 }
    );
  }
}
