package gc.mda.kcg.domain.analysis;

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;

/**
 * vessel_analysis_results 읽기 전용 Entity.
 * prediction 엔진이 5분 주기로 INSERT, 백엔드는 READ만 수행.
 *
 * DB PK는 (id, analyzed_at) 복합키(파티션)이지만,
 * BIGSERIAL id가 전역 유니크이므로 JPA에서는 id만 @Id로 매핑.
 */
@Entity
@Table(name = "vessel_analysis_results", schema = "kcg")
@Getter @NoArgsConstructor(access = AccessLevel.PROTECTED)
public class VesselAnalysisResult {

    @Id
    private Long id;

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

    @Column(name = "analyzed_at", nullable = false)
    private OffsetDateTime analyzedAt;

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

    @Column(name = "confidence", precision = 5, scale = 4)
    private BigDecimal confidence;

    @Column(name = "fishing_pct", precision = 5, scale = 4)
    private BigDecimal fishingPct;

    @Column(name = "cluster_id")
    private Integer clusterId;

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

    // 위치
    @Column(name = "lat")
    private Double lat;

    @Column(name = "lon")
    private Double lon;

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

    @Column(name = "dist_to_baseline_nm", precision = 8, scale = 2)
    private BigDecimal distToBaselineNm;

    // 행동 분석
    @Column(name = "activity_state", length = 20)
    private String activityState;

    @Column(name = "ucaf_score", precision = 5, scale = 4)
    private BigDecimal ucafScore;

    @Column(name = "ucft_score", precision = 5, scale = 4)
    private BigDecimal ucftScore;

    // 위협 탐지
    @Column(name = "is_dark")
    private Boolean isDark;

    @Column(name = "gap_duration_min")
    private Integer gapDurationMin;

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

    @Column(name = "spoofing_score", precision = 5, scale = 4)
    private BigDecimal spoofingScore;

    @Column(name = "bd09_offset_m", precision = 8, scale = 2)
    private BigDecimal bd09OffsetM;

    @Column(name = "speed_jump_count")
    private Integer speedJumpCount;

    // 환적
    @Column(name = "transship_suspect")
    private Boolean transshipSuspect;

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

    @Column(name = "transship_duration_min")
    private Integer transshipDurationMin;

    // 선단
    @Column(name = "fleet_cluster_id")
    private Integer fleetClusterId;

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

    @Column(name = "fleet_is_leader")
    private Boolean fleetIsLeader;

    // 위험도
    @Column(name = "risk_score")
    private Integer riskScore;

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

    // 확장
    @Column(name = "gear_code", length = 20)
    private String gearCode;

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

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

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

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