package de.thpeetz.kontor.models.comics;

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

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.Version;
import lombok.Getter;
import lombok.Setter;

@Entity
@Table
@Getter
@Setter
public class Comic {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private String id;

    @Version
    private int version;

    @Column(name = "created_date")
    private Date createdDate;

    @Column(name = "last_modified_date")
    private Date lastModifiedDate;

    @Column(unique = true, nullable = false)
    private String title;

    @ManyToOne
    @JoinColumn(name = "publisher_id", nullable = false)
    @JsonIgnoreProperties({ "comics" })
    private Publisher publisher;

    @Column(name = "current_order")
    private Boolean currentOrder = false;

    private Boolean completed = false;

    @Column(nullable = true)
    private String weblink;

    @Column(nullable = true)
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "comic", cascade = CascadeType.REFRESH, orphanRemoval = true)
    List<ComicWork> comicWorks;

    @Column(nullable = true)
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "comic", cascade = CascadeType.REFRESH, orphanRemoval = true)
    private List<Issue> issues = new LinkedList<>();

    @Column(nullable = true)
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "comic", cascade = CascadeType.REFRESH, orphanRemoval = true)
    private List<StoryArc> storyArcs = new LinkedList<>();

    @Column(nullable = true)
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "comic", cascade = CascadeType.REFRESH, orphanRemoval = true)
    private List<TradePaperback> tradePaperbacks = new LinkedList<>();

    @Column(nullable = true)
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "comic", cascade = CascadeType.REFRESH, orphanRemoval = true)
    private List<Volume> volumes = new LinkedList<>();

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Comic{");
        sb.append("title='").append(title).append('\'');
        sb.append(", publisher=").append(publisher);
        sb.append(", currentOrder=").append(currentOrder);
        sb.append(", completed=").append(completed);
        sb.append('}');
        return sb.toString();
    }

    public String getPublisherName() {
        return this.publisher.getName();
    }
}
