package com.bio.bio_backend.domain.admin.common_code.entity;

import com.bio.bio_backend.global.constants.AppConstants;
import com.bio.bio_backend.global.entity.BaseEntity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Comment;

@Entity
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(
    name = AppConstants.TABLE_PREFIX + "common_code",
    indexes = {
        @Index(name = "idx_common_code_code", columnList = "code"),
        @Index(name = "idx_common_code_group_code", columnList = "group_code"),
        @Index(name = "idx_common_code_parent_code", columnList = "parent_code")
    }
)
public class CommonCode extends BaseEntity {

    @Column(name = "code", nullable = false, length = 50, unique = true)
    @Comment("코드")
    private String code;

    @Column(name = "name", nullable = false, length = 100)
    @Comment("코드명")
    private String name;

    @Column(name = "description", length = 500)
    @Comment("설명")
    private String description;

    @Column(name = "group_code", nullable = false, length = 50)
    @Comment("그룹 코드")
    private String groupCode;

    @Column(name = "parent_code", length = 50)
    @Comment("부모 코드")
    private String parentCode;

    @Column(name = "character_ref1", length = 100)
    @Comment("문자 참조1")
    private String characterRef1;

    @Column(name = "character_ref2", length = 100)
    @Comment("문자 참조2")
    private String characterRef2;

    @Column(name = "character_ref3", length = 100)
    @Comment("문자 참조3")
    private String characterRef3;

    @Column(name = "character_ref4", length = 100)
    @Comment("문자 참조4")
    private String characterRef4;

    @Column(name = "character_ref5", length = 100)
    @Comment("문자 참조5")
    private String characterRef5;

    @Column(name = "sort_order", nullable = false)
    @Comment("정렬 순번")
    @Builder.Default
    private Integer sortOrder = 0;

    @Column(name = "use_flag", nullable = false)
    @Comment("사용 여부")
    @Builder.Default
    private Boolean useFlag = true;

    // 관계 설정
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(
        name = "group_code", 
        referencedColumnName = "code",
        insertable = false, 
        updatable = false,
        foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
    )
    private CommonGroupCode commonGroupCode;
}
