package de.thpeetz.kontor.comics.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.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

/**
 * Represents a comic entity.
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@Entity
public class Comic extends AbstractEntity {

    @NotEmpty
    @Column(unique = true)
    private String title;

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

    private Boolean currentOrder = false;

    private Boolean completed = false;

    @Nullable
    private String weblink;

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

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

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

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

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "comic", cascade = CascadeType.REFRESH, orphanRemoval = true)
    @Nullable
    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();
    }
}
