import BookDetails from '@app/components/BookDetails';
import axios from 'axios';
import type { GetServerSideProps, NextPage } from 'next';

interface BookPageProps {
  book?: any;
}

const BookPage: NextPage<BookPageProps> = ({ book }) => {
  return <BookDetails book={book} />;
};

export const getServerSideProps: GetServerSideProps<BookPageProps> = async (
  ctx
) => {
  try {
    const response = await axios.get(
      `http://${process.env.HOST || 'localhost'}:${
        process.env.PORT || 5055
      }/api/v1/book/search?query=${ctx.query.bookId}`,
      {
        headers: ctx.req?.headers?.cookie
          ? { cookie: ctx.req.headers.cookie }
          : undefined,
      }
    );
    return { props: { book: response.data?.results?.[0] } };
  } catch {
    return { props: {} };
  }
};

export default BookPage;
