package de.assecutor.votianlt.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.time.Instant;

/**
 * Represents a GPS position reported by a mobile client.
 */
@Data
@NoArgsConstructor
@Document(collection = "location_positions")
public class LocationPosition {

    @Id
    private ObjectId id;

    /**
     * AppUser ID (clientId) - the user who sent this position
     */
    @Field("app_user_id")
    @Indexed
    private String appUserId;

    /**
     * Latitude in decimal degrees
     */
    @Field("latitude")
    private Double latitude;

    /**
     * Longitude in decimal degrees
     */
    @Field("longitude")
    private Double longitude;

    /**
     * Accuracy of the position in meters
     */
    @Field("accuracy")
    private Double accuracy;

    /**
     * Altitude in meters above sea level (optional)
     */
    @Field("altitude")
    private Double altitude;

    /**
     * Speed in meters per second (optional)
     */
    @Field("speed")
    private Double speed;

    /**
     * Heading in degrees (0-360) (optional)
     */
    @Field("heading")
    private Double heading;

    /**
     * Timestamp when the position was reported (from client)
     */
    @Field("timestamp")
    private Instant timestamp;

    /**
     * Timestamp when the position was received by the server
     */
    @Field("received_at")
    @Indexed(expireAfter = "3600s") // TTL index: auto-delete after 60 minutes
    private Instant receivedAt;

    public LocationPosition(String appUserId, Double latitude, Double longitude, Double accuracy, Double altitude,
            Double speed, Double heading, Instant timestamp) {
        this.appUserId = appUserId;
        this.latitude = latitude;
        this.longitude = longitude;
        this.accuracy = accuracy;
        this.altitude = altitude;
        this.speed = speed;
        this.heading = heading;
        this.timestamp = timestamp;
        this.receivedAt = Instant.now();
    }
}
