package gc.mda.kcg.permission;

import jakarta.persistence.*;
import lombok.*;

import java.time.OffsetDateTime;

@Entity
@Table(name = "auth_role", schema = "kcg")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Role {

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

    @Column(name = "role_cd", nullable = false, unique = true, length = 50)
    private String roleCd;

    @Column(name = "role_nm", nullable = false, length = 100)
    private String roleNm;

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

    /** 역할 UI 표기 색상 (#RRGGBB). NULL 가능 — 프론트가 기본 팔레트 사용. */
    @Column(name = "color_hex", length = 7)
    private String colorHex;

    @Column(name = "dflt_yn", nullable = false, length = 1)
    private String dfltYn;

    @Column(name = "builtin_yn", nullable = false, length = 1)
    private String builtinYn;

    @Column(name = "created_at", nullable = false)
    private OffsetDateTime createdAt;

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

    @PrePersist
    void prePersist() {
        OffsetDateTime now = OffsetDateTime.now();
        if (createdAt == null) createdAt = now;
        if (updatedAt == null) updatedAt = now;
        if (dfltYn == null) dfltYn = "N";
        if (builtinYn == null) builtinYn = "N";
    }

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