package gc.mda.kcg.domain.aircraft;

import jakarta.persistence.*;
import lombok.*;
import org.locationtech.jts.geom.Point;

import java.time.Instant;

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

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

    @Column(nullable = false, length = 6)
    private String icao24;

    @Column(length = 16)
    private String callsign;

    @Column(columnDefinition = "geometry(Point, 4326)", nullable = false)
    private Point position;

    private Double altitude;
    private Double velocity;
    private Double heading;

    @Column(name = "vertical_rate")
    private Double verticalRate;

    @Column(name = "on_ground")
    private Boolean onGround;

    @Enumerated(EnumType.STRING)
    @Column(length = 16)
    private AircraftCategory category;

    @Column(length = 16)
    private String typecode;

    @Column(name = "type_desc", length = 128)
    private String typeDesc;

    @Column(length = 16)
    private String registration;

    @Column(length = 128)
    private String operator;

    @Column(length = 4)
    private String squawk;

    @Column(nullable = false, length = 16)
    private String source;

    @Column(nullable = false, length = 16)
    private String region;

    @Column(name = "collected_at", nullable = false)
    private Instant collectedAt;

    @Column(name = "last_seen")
    private Instant lastSeen;

    @PrePersist
    protected void onCreate() {
        if (collectedAt == null) {
            collectedAt = Instant.now();
        }
    }
}
