import { Book } from '@/books/Book';
import data from '@/data/books.json';

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

export async function GET(
  request: Request,
  { params }: RouteParams,
): Promise<Response> {
  const { id } = await params;

  const book = (data.books as Book[]).find(
    (book) => book.id === parseInt(id, 10),
  );

  if (!book) {
    return new Response(JSON.stringify({ error: 'Book not found' }), {
      status: 404,
    });
  }
  return new Response(JSON.stringify(book));
}
