package de.assecutor.votianlt.repository;

import de.assecutor.votianlt.model.LocationPosition;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.util.List;

/**
 * Repository for location positions reported by mobile clients.
 */
@Repository
public interface LocationPositionRepository extends MongoRepository<LocationPosition, ObjectId> {

    /**
     * Find all positions for a specific app user, ordered by timestamp descending
     */
    List<LocationPosition> findByAppUserIdOrderByTimestampDesc(String appUserId);

    /**
     * Find all positions received after a specific time
     */
    List<LocationPosition> findByReceivedAtAfterOrderByTimestampDesc(Instant receivedAt);

    /**
     * Find all positions for a user received after a specific time
     */
    List<LocationPosition> findByAppUserIdAndReceivedAtAfterOrderByTimestampDesc(String appUserId, Instant receivedAt);

    /**
     * Delete all positions older than the specified timestamp
     */
    void deleteByReceivedAtBefore(Instant cutoff);

    /**
     * Find latest position for a specific app user
     */
    List<LocationPosition> findTop1ByAppUserIdOrderByTimestampDesc(String appUserId);
}
