package com.emonster.taroaichat.domain;

import com.emonster.taroaichat.domain.enumeration.Feedback;
import com.emonster.taroaichat.domain.enumeration.SessionStatus;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import java.io.Serializable;
import java.time.Instant;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.domain.Persistable;

/**
 * Tarot reading sessions
 */
@Entity
@Table(name = "tarot_session")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIgnoreProperties(value = { "new" })
@SuppressWarnings("common-java:DuplicatedBlocks")
public class TarotSession extends AbstractAuditingEntity<Long> implements Serializable, Persistable<Long> {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Lob
    @Column(name = "cards")
    private String cards;

    @Lob
    @Column(name = "interpretation")
    private String interpretation;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false)
    private SessionStatus status;

    @Enumerated(EnumType.STRING)
    @Column(name = "feedback")
    private Feedback feedback;

    @Min(value = 1)
    @Max(value = 5)
    @Column(name = "rating")
    private Integer rating;

    @NotNull
    @Column(name = "saved", nullable = false)
    private Boolean saved;

    @Lob
    @Column(name = "summary")
    private String summary;

    @Size(max = 500)
    @Column(name = "screenshot_url", length = 500)
    private String screenshotUrl;

    @Column(name = "completed_at")
    private Instant completedAt;

    // Inherited createdBy definition
    // Inherited createdDate definition
    // Inherited lastModifiedBy definition
    // Inherited lastModifiedDate definition
    @org.springframework.data.annotation.Transient
    @Transient
    private boolean isPersisted;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "session")
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @JsonIgnoreProperties(value = { "session" }, allowSetters = true)
    private Set<ChatMessage> chatMessages = new HashSet<>();

    @ManyToOne(optional = false)
    @NotNull
    @JsonIgnoreProperties(value = { "user", "tarotSessions", "donations" }, allowSetters = true)
    private UserProfile userProfile;

    // jhipster-needle-entity-add-field - JHipster will add fields here

    public Long getId() {
        return this.id;
    }

    public TarotSession id(Long id) {
        this.setId(id);
        return this;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCards() {
        return this.cards;
    }

    public TarotSession cards(String cards) {
        this.setCards(cards);
        return this;
    }

    public void setCards(String cards) {
        this.cards = cards;
    }

    public String getInterpretation() {
        return this.interpretation;
    }

    public TarotSession interpretation(String interpretation) {
        this.setInterpretation(interpretation);
        return this;
    }

    public void setInterpretation(String interpretation) {
        this.interpretation = interpretation;
    }

    public SessionStatus getStatus() {
        return this.status;
    }

    public TarotSession status(SessionStatus status) {
        this.setStatus(status);
        return this;
    }

    public void setStatus(SessionStatus status) {
        this.status = status;
    }

    public Feedback getFeedback() {
        return this.feedback;
    }

    public TarotSession feedback(Feedback feedback) {
        this.setFeedback(feedback);
        return this;
    }

    public void setFeedback(Feedback feedback) {
        this.feedback = feedback;
    }

    public Integer getRating() {
        return this.rating;
    }

    public TarotSession rating(Integer rating) {
        this.setRating(rating);
        return this;
    }

    public void setRating(Integer rating) {
        this.rating = rating;
    }

    public Boolean getSaved() {
        return this.saved;
    }

    public TarotSession saved(Boolean saved) {
        this.setSaved(saved);
        return this;
    }

    public void setSaved(Boolean saved) {
        this.saved = saved;
    }

    public String getSummary() {
        return this.summary;
    }

    public TarotSession summary(String summary) {
        this.setSummary(summary);
        return this;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getScreenshotUrl() {
        return this.screenshotUrl;
    }

    public TarotSession screenshotUrl(String screenshotUrl) {
        this.setScreenshotUrl(screenshotUrl);
        return this;
    }

    public void setScreenshotUrl(String screenshotUrl) {
        this.screenshotUrl = screenshotUrl;
    }

    public Instant getCompletedAt() {
        return this.completedAt;
    }

    public TarotSession completedAt(Instant completedAt) {
        this.setCompletedAt(completedAt);
        return this;
    }

    public void setCompletedAt(Instant completedAt) {
        this.completedAt = completedAt;
    }

    // Inherited createdBy methods
    public TarotSession createdBy(String createdBy) {
        this.setCreatedBy(createdBy);
        return this;
    }

    // Inherited createdDate methods
    public TarotSession createdDate(Instant createdDate) {
        this.setCreatedDate(createdDate);
        return this;
    }

    // Inherited lastModifiedBy methods
    public TarotSession lastModifiedBy(String lastModifiedBy) {
        this.setLastModifiedBy(lastModifiedBy);
        return this;
    }

    // Inherited lastModifiedDate methods
    public TarotSession lastModifiedDate(Instant lastModifiedDate) {
        this.setLastModifiedDate(lastModifiedDate);
        return this;
    }

    @PostLoad
    @PostPersist
    public void updateEntityState() {
        this.setIsPersisted();
    }

    @org.springframework.data.annotation.Transient
    @Transient
    @Override
    public boolean isNew() {
        return !this.isPersisted;
    }

    public TarotSession setIsPersisted() {
        this.isPersisted = true;
        return this;
    }

    public Set<ChatMessage> getChatMessages() {
        return this.chatMessages;
    }

    public void setChatMessages(Set<ChatMessage> chatMessages) {
        if (this.chatMessages != null) {
            this.chatMessages.forEach(i -> i.setSession(null));
        }
        if (chatMessages != null) {
            chatMessages.forEach(i -> i.setSession(this));
        }
        this.chatMessages = chatMessages;
    }

    public TarotSession chatMessages(Set<ChatMessage> chatMessages) {
        this.setChatMessages(chatMessages);
        return this;
    }

    public TarotSession addChatMessage(ChatMessage chatMessage) {
        this.chatMessages.add(chatMessage);
        chatMessage.setSession(this);
        return this;
    }

    public TarotSession removeChatMessage(ChatMessage chatMessage) {
        this.chatMessages.remove(chatMessage);
        chatMessage.setSession(null);
        return this;
    }

    public UserProfile getUserProfile() {
        return this.userProfile;
    }

    public void setUserProfile(UserProfile userProfile) {
        this.userProfile = userProfile;
    }

    public TarotSession userProfile(UserProfile userProfile) {
        this.setUserProfile(userProfile);
        return this;
    }

    // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof TarotSession)) {
            return false;
        }
        return getId() != null && getId().equals(((TarotSession) o).getId());
    }

    @Override
    public int hashCode() {
        // see https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
        return getClass().hashCode();
    }

    // prettier-ignore
    @Override
    public String toString() {
        return "TarotSession{" +
            "id=" + getId() +
            ", cards='" + getCards() + "'" +
            ", interpretation='" + getInterpretation() + "'" +
            ", status='" + getStatus() + "'" +
            ", feedback='" + getFeedback() + "'" +
            ", rating=" + getRating() +
            ", saved='" + getSaved() + "'" +
            ", summary='" + getSummary() + "'" +
            ", screenshotUrl='" + getScreenshotUrl() + "'" +
            ", completedAt='" + getCompletedAt() + "'" +
            ", createdBy='" + getCreatedBy() + "'" +
            ", createdDate='" + getCreatedDate() + "'" +
            ", lastModifiedBy='" + getLastModifiedBy() + "'" +
            ", lastModifiedDate='" + getLastModifiedDate() + "'" +
            "}";
    }
}
