package com.lanyuanxiaoyao.bookstore.entity;

import com.lanyuanxiaoyao.bookstore.Constants;
import com.lanyuanxiaoyao.service.template.entity.SimpleEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.FieldNameConstants;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

/**
 * 书籍
 *
 * @author lanyuanxiaoyao
 * @version 20250922
 */
@Setter
@Getter
@ToString(callSuper = true)
@FieldNameConstants
@Entity
@DynamicUpdate
@DynamicInsert
@EntityListeners(AuditingEntityListener.class)
@Table(name = Constants.DATABASE_PREFIX + "book")
public class Book extends SimpleEntity {
  @Column(nullable = false)
  private String name;
  private String author;
  private String description;
  private String source;
  @ElementCollection(fetch = FetchType.EAGER)
  @CollectionTable(name = Constants.DATABASE_PREFIX + "book_tags", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
  private Set<String> tags;

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "book")
  @ToString.Exclude
  private Set<Chapter> chapters;
}
