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.OneToMany;
import jakarta.persistence.Table;
import java.time.OffsetDateTime;
import java.util.Objects;
import java.util.Set;
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 = "project")
public class ProjectEntity {

  @Id
  @UuidGenerator
  private UUID id;

  @Column(name = "source_id", nullable = false, length = 36)
  private UUID sourceId;
  
  @Column(name = "title", nullable = false, length = 255)
  private String title;
  
  @Column(name = "comment", nullable = true)
  private String comment;

  @OneToMany(mappedBy = "project", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @ToString.Exclude
  @EqualsAndHashCode.Exclude
  private Set<MessageEntity> message;

  @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;
    }
    ProjectEntity that = (ProjectEntity) o;
    return id != null && Objects.equals(id, that.getId());
  }

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