package ru.ibs.socrates.confexporter.dao.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import java.time.OffsetDateTime;
import java.util.Objects;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.Hibernate;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.annotations.UuidGenerator;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "text_content")
public class TextContentEntity {

  @Id
  @UuidGenerator
  private UUID id;

  @OneToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.ALL)
  @JoinColumn(name = "message_id", referencedColumnName = "id")
  private MessageEntity message;

  @Column(name = "text", columnDefinition = "TEXT", nullable = false)
  @ToString.Exclude
  @EqualsAndHashCode.Exclude
  private String text;

  /**
   * url ссылка на содержимое
   */
  @Column(name = "link", nullable = false)
  private String link;

  @CreationTimestamp
  @Column(name = "created", updatable = false, nullable = false)
  private OffsetDateTime created;

  @UpdateTimestamp
  @Column(name = "updated", nullable = false)
  private OffsetDateTime updated;

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
      return false;
    }
    TextContentEntity that = (TextContentEntity) o;
    return id != null && Objects.equals(id, that.getId());
  }

  @Override
  public int hashCode() {
    return getClass().hashCode();
  }
}
