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 { code, name, country, isActive } = body;
    const item = await prisma.airline.update({
      where: { id: parseInt(id) },
      data: { code: code?.toUpperCase().trim(), name: name?.trim(), country: country?.trim() || null, isActive }
    });
    return NextResponse.json(item);
  } catch (error: any) {
    if (error.code === 'P2002') return NextResponse.json({ error: 'Código duplicado' }, { status: 409 });
    return NextResponse.json({ error: 'Error updating' }, { status: 500 });
  }
}

export async function DELETE(_: Request, { params }: Params) {
  const { id } = await params;
  try {
    await prisma.airline.delete({ where: { id: parseInt(id) } });
    return NextResponse.json({ ok: true });
  } catch {
    return NextResponse.json({ error: 'Error deleting' }, { status: 500 });
  }
}
