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

type Params = { params: Promise<{ id: string }> };

export async function PUT(request: Request, { params }: Params) {
  const { id } = await params;
  try {
    const body = await request.json();
    const { daeNumber, farmId, exportDate, status, totalBoxes, totalWeight, airlineCode, flightNumber, destination, notes } = body;
    const dae = await prisma.dae.update({
      where: { id },
      data: {
        daeNumber,
        farmId: farmId || null,
        exportDate: exportDate ? new Date(exportDate) : null,
        status,
        totalBoxes: totalBoxes ? parseInt(totalBoxes) : null,
        totalWeight: totalWeight ? parseFloat(totalWeight) : null,
        airlineCode: airlineCode || null,
        flightNumber: flightNumber || null,
        destination: destination || null,
        notes: notes || null,
      },
    });
    return NextResponse.json(dae);
  } catch {
    return NextResponse.json({ error: 'Error al actualizar' }, { status: 500 });
  }
}

export async function DELETE(request: Request, { params }: Params) {
  const { id } = await params;
  try {
    await prisma.dae.delete({ where: { id } });
    return NextResponse.json({ success: true });
  } catch {
    return NextResponse.json({ error: 'Error al eliminar' }, { status: 500 });
  }
}
