import { z } from 'zod/v4';

const BookSchema = z.object({
  id: z.number(),
  title: z.string(),
  author: z.string().optional(),
  isbn: z.string(),
  rating: z.number().optional(),
});

function BooksListItem({ book }) {
  const result = BookSchema.safeParse(book);

  if (!result.success) {
    console.error('Invalid book prop:', result.error);
    return null;
  }

  const validBook = result.data;

  return (
    <tr>
      <td>{validBook.title}</td>
      <td>{validBook.author ? validBook.author : 'Unbekannt'}</td>
      <td>{validBook.isbn}</td>
      <td>{validBook.rating && <span>{'*'.repeat(validBook.rating)}</span>}</td>
    </tr>
  );
}

export default BooksListItem;
