package gc.mda.kcg.domain.enforcement;

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.UUID;

/**
 * 단속 이력.
 * 실제 단속 수행 기록을 저장.
 */
@Entity
@Table(name = "enforcement_records", schema = "kcg",
        uniqueConstraints = @UniqueConstraint(columnNames = "enf_uid"))
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
public class EnforcementRecord {

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

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

    @Column(name = "event_id")
    private Long eventId;

    @Column(name = "enforced_at")
    private OffsetDateTime enforcedAt;

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

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

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

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

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

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

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

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

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

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

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

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

    @Column(name = "patrol_ship_id")
    private Long patrolShipId;

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "enforced_by")
    private UUID enforcedBy;

    @Column(name = "enforced_by_name", length = 100)
    private String enforcedByName;

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

    @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;
    }

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