package gc.mda.kcg.domain.analysis;

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

import java.time.Instant;
import java.util.Map;

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

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 15)
    private String mmsi;

    @Column(nullable = false)
    private Instant timestamp;

    @Column(length = 20)
    private String vesselType;

    private Double confidence;

    private Double fishingPct;

    private Integer clusterId;

    @Column(length = 10)
    private String season;

    @Column(length = 20)
    private String zone;

    private Double distToBaselineNm;

    @Column(length = 20)
    private String activityState;

    private Double ucafScore;

    private Double ucftScore;

    @Column(nullable = false)
    private Boolean isDark;

    private Integer gapDurationMin;

    private Double spoofingScore;

    private Double bd09OffsetM;

    private Integer speedJumpCount;

    private Integer clusterSize;

    @Column(nullable = false)
    private Boolean isLeader;

    @Column(length = 20)
    private String fleetRole;

    private Integer riskScore;

    @Column(length = 20)
    private String riskLevel;

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(columnDefinition = "jsonb")
    private Map<String, Double> features;

    @Column(nullable = false)
    private Instant analyzedAt;

    @PrePersist
    protected void onCreate() {
        if (analyzedAt == null) {
            analyzedAt = Instant.now();
        }
        if (isDark == null) {
            isDark = false;
        }
        if (isLeader == null) {
            isLeader = false;
        }
    }
}
