package gc.mda.kcg.permission;

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

import java.time.OffsetDateTime;

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

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

    @Column(name = "parent_cd", length = 100)
    private String parentCd;

    @Column(name = "rsrc_nm", nullable = false, length = 100)
    private String rsrcNm;

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

    @Column(name = "icon", length = 50)
    private String icon;

    @Column(name = "rsrc_level", nullable = false)
    private Integer rsrcLevel;

    @Column(name = "sort_ord", nullable = false)
    private Integer sortOrd;

    @Column(name = "use_yn", nullable = false, length = 1)
    private String useYn;

    // ── 메뉴 SSOT 컬럼 (V021) ──
    @Column(name = "url_path", length = 200)
    private String urlPath;

    @Column(name = "label_key", length = 100)
    private String labelKey;

    @Column(name = "component_key", length = 150)
    private String componentKey;

    @Column(name = "nav_group", length = 100)
    private String navGroup;

    @Column(name = "nav_sub_group", length = 100)
    private String navSubGroup;

    @Column(name = "nav_sort", nullable = false)
    private Integer navSort;

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "labels", columnDefinition = "jsonb")
    private java.util.Map<String, String> labels;

    @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 (useYn == null) useYn = "Y";
        if (sortOrd == null) sortOrd = 0;
        if (rsrcLevel == null) rsrcLevel = 0;
        if (navSort == null) navSort = 0;
    }

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