package gc.mda.kcg.domain.enforcement;

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

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.UUID;

/**
 * 단속 계획.
 * 향후 단속 예정 계획을 관리.
 */
@Entity
@Table(name = "enforcement_plans", schema = "kcg",
        uniqueConstraints = @UniqueConstraint(columnNames = "plan_uid"))
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
public class EnforcementPlan {

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

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

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

    @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 = "planned_date")
    private LocalDate plannedDate;

    @Column(name = "planned_from")
    private OffsetDateTime plannedFrom;

    @Column(name = "planned_to")
    private OffsetDateTime plannedTo;

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

    @Column(name = "risk_score")
    private Integer riskScore;

    @Column(name = "assigned_ship_count")
    private Integer assignedShipCount;

    @Column(name = "assigned_crew")
    private Integer assignedCrew;

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

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

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "created_by")
    private UUID createdBy;

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "approved_by")
    private UUID approvedBy;

    @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;
        if (status == null) status = "DRAFT";
    }

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