package gc.mda.kcg.domain.event;

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;

/**
 * AI 예측 이벤트.
 * 불법어선 탐지, 이상행위 감지 등 시스템이 생성한 이벤트를 저장.
 */
@Entity
@Table(name = "prediction_events", schema = "kcg",
        uniqueConstraints = @UniqueConstraint(columnNames = "event_uid"))
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
public class PredictionEvent {

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

    @Column(name = "event_uid", nullable = false, length = 50, unique = true)
    private String eventUid;

    @Column(name = "occurred_at")
    private OffsetDateTime occurredAt;

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

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

    @Column(name = "title", length = 200)
    private String title;

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

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

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

    @Column(name = "area_name", length = 100)
    private String areaName;

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

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

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

    @Column(name = "speed_kn", precision = 5, scale = 2)
    private BigDecimal speedKn;

    @Column(name = "source_type", length = 50)
    private String sourceType;

    @Column(name = "source_ref_id")
    private Long sourceRefId;

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

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

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "assignee_id")
    private UUID assigneeId;

    @Column(name = "assignee_name", length = 100)
    private String assigneeName;

    @Column(name = "acked_at")
    private OffsetDateTime ackedAt;

    @Column(name = "resolved_at")
    private OffsetDateTime resolvedAt;

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

    @Column(name = "dedup_key", length = 200)
    private String dedupKey;

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

    @Column(name = "created_at", nullable = false)
    private OffsetDateTime createdAt;

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

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

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