package de.thpeetz.kontor.models.comics;

import java.time.YearMonth;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.hypersistence.utils.hibernate.type.basic.YearMonthDateType;
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;

import org.hibernate.annotations.Type;

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

    @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;

    @ManyToOne
    @JoinColumn(name = "comic_id", nullable = false)
    @JsonIgnoreProperties({ "issues" })
    private Comic comic;

    @ManyToOne
    @JoinColumn(name = "volume_id", nullable = true)
    @JsonIgnoreProperties({ "issues" })
    private Volume volume;

    @ManyToOne
    @JoinColumn(name = "story_arc_id", nullable = true)
    @JsonIgnoreProperties({ "issues" })
    private StoryArc storyArc;

    @Column(name = "issue_number", nullable = false)
    private String issueNumber;

    @Column(nullable = true)
    private String title;

    @Type(YearMonthDateType.class)
    @Column(name = "published_on", columnDefinition = "date", nullable = true)
    private YearMonth publishedOn;

    @Column(name = "is_read")
    private Boolean isRead;

    @Column(name = "in_stock")
    private Boolean inStock;

    @Column(nullable = true)
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "issue", cascade = CascadeType.REFRESH, orphanRemoval = true)
    List<IssueWork> issueWorks;

    public String getComicTitle() {
        return comic.getTitle();
    }

    public String getVolumeName() {
        if (volume != null) {
            return volume.getName();
        }
        return null;
    }

    public String getFullTitle() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(this.getComic().getTitle());
        stringBuilder.append(" #");
        stringBuilder.append(this.getIssueNumber());
        if (this.title != null && !this.title.isEmpty()) {
            stringBuilder.append(": ");
            stringBuilder.append(this.title);
        }
        return stringBuilder.toString();
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append("Issue{");
        result.append("comic=");
        result.append(comic);
        result.append(", volume=");
        result.append(volume);
        result.append(", storyArc=");
        result.append(storyArc);
        result.append(", issueNumber='");
        result.append(issueNumber);
        result.append(", title='");
        result.append(title);
        result.append(", publishedOn=");
        result.append(publishedOn);
        result.append(", isRead=");
        result.append(isRead);
        result.append(", inStock=");
        result.append(inStock);
        result.append("}");
        return result.toString();
    }
}
