package com.example.backend.books.model;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.Date;

import com.example.backend.categories.model.CategoryEntity;
import com.example.backend.ordersbooks.model.OrderBookEntity;
import com.example.backend.core.model.BaseEntity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Lob;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

@Entity
@Table(name = "books")
public class BookEntity extends BaseEntity {
    @ManyToOne
    @JoinColumn(name = "categoryId", nullable = false)
    private CategoryEntity category;
    @Column(nullable = false)
    private String title;
    @Column(nullable = false)
    private String author;
    @Column(nullable = false)
    private Double price;
    @Lob
    private String description;
    @Lob
    private String annotation;
    @Column(nullable = false, unique = true)
    private String bookCode;
    private String publisher;
    private String series;
    @Column(nullable = false)
    private Integer publicationYear;
    private Integer pagesNum;
    private String size;
    private String coverType;
    private Integer circulation;
    private Integer weight;
    @Lob
    private String image;
    private Date date = new Date();

    @OneToMany(mappedBy = "book", cascade = CascadeType.REMOVE)
    @OrderBy("id ASC")
    private Set<OrderBookEntity> orderBooks = new HashSet<>();

    public BookEntity() {
    }

    public BookEntity(CategoryEntity category, String title, String author, Double price,
            String description, String annotation, String bookCode, String publisher, String series,
            Integer publicationYear, Integer pagesNum, String size, String coverType, Integer circulation,
            Integer weight, String image) {
        this.category = category;
        this.title = title;
        this.author = author;
        this.price = price;
        this.description = description;
        this.annotation = annotation;
        this.bookCode = bookCode;
        this.publisher = publisher;
        this.series = series;
        this.publicationYear = publicationYear;
        this.pagesNum = pagesNum;
        this.size = size;
        this.coverType = coverType;
        this.circulation = circulation;
        this.weight = weight;
        this.image = image;
        date = new Date();
    }

    public CategoryEntity getCategory() {
        return category;
    }

    public void setCategory(CategoryEntity category) {
        this.category = category;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getAnnotation() {
        return annotation;
    }

    public void setAnnotation(String annotation) {
        this.annotation = annotation;
    }

    public String getBookCode() {
        return bookCode;
    }

    public void setBookCode(String bookCode) {
        this.bookCode = bookCode;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getSeries() {
        return series;
    }

    public void setSeries(String series) {
        this.series = series;
    }

    public Integer getPublicationYear() {
        return publicationYear;
    }

    public void setPublicationYear(Integer publicationYear) {
        this.publicationYear = publicationYear;
    }

    public Integer getPagesNum() {
        return pagesNum;
    }

    public void setPagesNum(Integer pagesNum) {
        this.pagesNum = pagesNum;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getCoverType() {
        return coverType;
    }

    public void setCoverType(String coverType) {
        this.coverType = coverType;
    }

    public Integer getCirculation() {
        return circulation;
    }

    public void setCirculation(Integer circulation) {
        this.circulation = circulation;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Set<OrderBookEntity> getOrderBooks() {
        return orderBooks;
    }

    public void addOrder(OrderBookEntity orderBook) {
        if (orderBook.getBook() != this) {
            orderBook.setBook(this);
        }
        orderBooks.add(orderBook);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, category, title, author, price, description, annotation, bookCode, publisher,
                series, publicationYear, pagesNum, size, coverType, circulation, weight, image);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null || getClass() != obj.getClass())
            return false;
        final BookEntity other = (BookEntity) obj;
        return Objects.equals(other.getId(), id)
                && Objects.equals(other.getCategory(), category)
                && Objects.equals(other.getTitle(), title)
                && Objects.equals(other.getAuthor(), author)
                && Objects.equals(other.getPrice(), price)
                && Objects.equals(other.getDescription(), description)
                && Objects.equals(other.getAnnotation(), annotation)
                && Objects.equals(other.getBookCode(), bookCode)
                && Objects.equals(other.getPublisher(), publisher)
                && Objects.equals(other.getSeries(), series)
                && Objects.equals(other.getPublicationYear(), publicationYear)
                && Objects.equals(other.getPagesNum(), pagesNum)
                && Objects.equals(other.getSize(), size)
                && Objects.equals(other.getCoverType(), coverType)
                && Objects.equals(other.getCirculation(), circulation)
                && Objects.equals(other.getWeight(), weight)
                && Objects.equals(other.getImage(), image);
    }
}
