import { revalidatePath } from 'next/cache';

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

export async function DELETE(request: Request, { params }: RouteParams) {
  try {
    const { id } = await params;

    const response = await fetch(`http://localhost:3001/books/${id}`, {
      method: 'DELETE',
    });

    if (!response.ok) {
      return new Response('Failed to delete book', { status: response.status });
    }

    revalidatePath('/books');

    return new Response(null, { status: 204 });
  } catch {
    return new Response('Internal Server Error', { status: 500 });
  }
}
