package com.bio.bio_backend.global.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

import static com.bio.bio_backend.global.utils.OidUtil.generateOid;

/**
 * 모든 엔티티가 상속받는 기본 엔티티 클래스
 * 공통 필드들을 정의하고 JPA Auditing을 지원합니다.
 */
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {

    @Id
    @Column(name = "oid", nullable = false)
    private Long oid;

    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    @Column(name = "created_oid", updatable = false)
    private Long createdOid;

    @Column(name = "updated_oid")
    private Long updatedOid;
    
    @PrePersist
    protected void onCreate() {
        if(this.oid == null) this.oid = generateOid();
        if(this.createdOid != null && this.updatedOid == null) this.updatedOid = this.createdOid;
    }
}
