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 Volume {

    @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(nullable = false)
    private String name;

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

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

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

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder("Volume{");
        result.append("name=");
        result.append(name);
        result.append("comic=Comic{title=");
        result.append(comic.getTitle());
        result.append("}}");
        return result.toString();
    }
}
