package com.kamco.cd.training.postgres.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.ZonedDateTime;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;

@Getter
@Setter
@Entity
@Table(name = "tb_learn_data")
public class LearnDataEntity {

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

  @Size(max = 50)
  @NotNull
  @Column(name = "category", nullable = false, length = 50)
  private String category;

  @Size(max = 200)
  @Column(name = "title", length = 200)
  private String title;

  @Column(name = "episode")
  private Long episode;

  @Size(max = 20)
  @Column(name = "capacity", length = 20)
  private String capacity;

  @Column(name = "memo", columnDefinition = "text")
  private String memo;

  @Size(max = 20)
  @NotNull
  @Column(name = "status", nullable = false, length = 20)
  private String status;

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

  @NotNull
  @ColumnDefault("now()")
  @Column(name = "created_dttm", nullable = false)
  private ZonedDateTime createdDttm = ZonedDateTime.now();

  @Column(name = "updated_dttm")
  private ZonedDateTime updatedDttm;

  @PrePersist
  protected void onPersist() {
    if (this.createdDttm == null) {
      this.createdDttm = ZonedDateTime.now();
    }
    if (this.deleted == null) {
      this.deleted = false;
    }
  }

  @PreUpdate
  protected void onUpdate() {
    this.updatedDttm = ZonedDateTime.now();
  }
}
