import { Book } from '@/books/Book';
import { NextPage } from 'next/dist/types';
import Link from 'next/link';
import { notFound } from 'next/navigation';

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

async function getBook(id: string): Promise<Book> {
  const response = await fetch(`http://localhost:3001/books/${id}`);
  if (!response.ok) {
    if (response.status === 404) {
      notFound();
    }
    throw new Error('Fehler beim Laden des Buchs');
  }
  return response.json();
}

const BookDetailPage: NextPage<Props> = async ({ params }) => {
  const { id } = await params;

  const book = await getBook(id);

  return (
    <div>
      <h1>{book.title}</h1>
      <p>Autor: {book.author}</p>
      <p>ISBN: {book.isbn}</p>
      <Link href="/books">Zurück zur Übersicht</Link>
    </div>
  );
};

export default BookDetailPage;
