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.LocalDate;
import java.time.OffsetDateTime;

/**
 * 선박 허가 마스터.
 * 어선 허가 정보, 허용 어구/구역, 유효기간 등 관리.
 */
@Entity
@Table(name = "vessel_permit_master", schema = "kcg")
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
public class VesselPermit {

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

    @Column(name = "vessel_name", length = 100)
    private String vesselName;

    @Column(name = "vessel_name_cn", length = 100)
    private String vesselNameCn;

    @Column(name = "flag_country", length = 10)
    private String flagCountry;

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

    @Column(name = "tonnage")
    private BigDecimal tonnage;

    @Column(name = "length_m")
    private BigDecimal lengthM;

    @Column(name = "build_year")
    private Integer buildYear;

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

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

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

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

    @Column(name = "permit_valid_from")
    private LocalDate permitValidFrom;

    @Column(name = "permit_valid_to")
    private LocalDate permitValidTo;

    @Column(name = "company_id")
    private Long companyId;

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

    @Column(name = "last_synced_at")
    private OffsetDateTime lastSyncedAt;

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

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

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