package gc.mda.kcg.master;

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

import java.time.OffsetDateTime;
import java.util.Map;

/**
 * 계층형 코드 마스터.
 * 시스템 전반에서 사용하는 분류 코드를 트리 구조로 관리.
 */
@Entity
@Table(name = "code_master", schema = "kcg")
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
public class CodeMaster {

    @Id
    @Column(name = "code_id", length = 100)
    private String codeId;

    @Column(name = "parent_id", length = 100)
    private String parentId;

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

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

    @Column(name = "depth")
    private Integer depth;

    @Column(name = "name_ko", length = 100)
    private String nameKo;

    @Column(name = "name_en", length = 100)
    private String nameEn;

    @Column(name = "sort_order")
    private Integer sortOrder;

    @Column(name = "color_hex", length = 10)
    private String colorHex;

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

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

    @Column(name = "is_active")
    private Boolean isActive;

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

    @PrePersist
    void prePersist() {
        if (createdAt == null) createdAt = OffsetDateTime.now();
        if (isActive == null) isActive = true;
    }
}
