package gc.mda.kcg.master;

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

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.Map;
import java.util.UUID;

/**
 * 어구 유형 마스터.
 * 어구별 속도/패턴/법적 허용 구역 등 분석에 필요한 메타데이터 관리.
 */
@Entity
@Table(name = "gear_type_master", schema = "kcg")
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
public class GearType {

    @Id
    @Column(name = "gear_code", length = 20)
    private String gearCode;

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

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

    @Column(name = "category", length = 20)
    private String category;

    @Column(name = "speed_min_kn")
    private BigDecimal speedMinKn;

    @Column(name = "speed_max_kn")
    private BigDecimal speedMaxKn;

    @Column(name = "duration_min_minutes")
    private Integer durationMinMinutes;

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

    @Column(name = "polygon_shape_hint", length = 20)
    private String polygonShapeHint;

    @JdbcTypeCode(SqlTypes.ARRAY)
    @Column(name = "legal_zones", columnDefinition = "text[]")
    private String[] legalZones;

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

    @Column(name = "permit_required")
    private Boolean permitRequired;

    @Column(name = "display_color", length = 7)
    private String displayColor;

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

    @Column(name = "display_order")
    private Integer displayOrder;

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

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

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "created_by")
    private UUID createdBy;

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

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

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