package gc.mda.kcg.permission;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.OffsetDateTime;
import java.util.UUID;

@Entity
@Table(name = "auth_perm", schema = "kcg",
        uniqueConstraints = @UniqueConstraint(columnNames = {"role_sn", "rsrc_cd", "oper_cd"}))
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Perm {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "perm_sn")
    private Long permSn;

    @Column(name = "role_sn", nullable = false)
    private Long roleSn;

    @Column(name = "rsrc_cd", nullable = false, length = 100)
    private String rsrcCd;

    @Column(name = "oper_cd", nullable = false, length = 20)
    private String operCd;  // READ/CREATE/UPDATE/DELETE/EXPORT/MANAGE

    @Column(name = "grant_yn", nullable = false, length = 1)
    private String grantYn;  // Y / N

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

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "updated_by")
    private UUID updatedBy;

    @PrePersist
    @PreUpdate
    void preUpdate() {
        updatedAt = OffsetDateTime.now();
    }
}
