package com.mvrent.carrental.model;

import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;

/** The latest known GPS position / telematics state for a car (one row per car). */
@Entity
@Table(name = "vehicle_locations")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class VehicleLocation {

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

    @OneToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "car_id", unique = true)
    private Car car;

    private double latitude;

    private double longitude;

    /** Speed in km/h. */
    private Double speedKph;

    /** Heading in degrees (0–360). */
    private Double heading;

    private Integer odometer;

    private Boolean ignitionOn;

    private LocalDateTime updatedAt;

    /** Considered "live" if a ping arrived in the last 5 minutes. */
    @Transient
    public boolean isLive() {
        return updatedAt != null && updatedAt.isAfter(LocalDateTime.now().minusMinutes(5));
    }
}
