package de.thpeetz.kontor.bookshelf.data;

import java.util.LinkedList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import de.thpeetz.kontor.common.data.AbstractEntity;
import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Entity
@Table(indexes = @Index(columnList = "title, isbn"), uniqueConstraints = @UniqueConstraint(columnNames = {"isbn"}))
public class Book extends AbstractEntity {

    @NotEmpty
    private String title;

    @NotEmpty
    private String isbn;

    @Nullable
    private int year;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "book")
    @Nullable
    private List<BookAuthor> authors = new LinkedList<>();

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "publisher_id")
    @NotNull
    @JsonIgnoreProperties({ "books" })
    private BookshelfPublisher publisher;
}
