package com.kamco.cd.training.postgres;

import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;
import java.time.ZonedDateTime;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

@Getter
@MappedSuperclass
public class CommonDateEntity {

  @CreatedDate
  @Column(name = "created_dttm", updatable = false, nullable = false)
  private ZonedDateTime createdDate;

  @LastModifiedDate
  @Column(name = "updated_dttm", nullable = false)
  private ZonedDateTime modifiedDate;

  @PrePersist
  protected void onPersist() {
    this.createdDate = ZonedDateTime.now();
    this.modifiedDate = ZonedDateTime.now();
  }

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