package de.votian.services.repository;

import de.votian.services.entity.ServiceZipCodeHistory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;

public interface ServiceZipCodeHistoryRepository extends JpaRepository<ServiceZipCodeHistory, Long> {

    @Query("SELECT h FROM ServiceZipCodeHistory h WHERE h.fromZipCodeId = :fromZipCodeId " +
           "AND h.toZipCodeId = :toZipCodeId AND h.customerId = :customerId AND h.headquartersId = :headquartersId " +
           "AND h.vehicleTypeId = :vehicleTypeId AND h.validFrom <= :dateTime ORDER BY h.validFrom DESC")
    List<ServiceZipCodeHistory> findLatest(@Param("fromZipCodeId") Long fromZipCodeId,
                                           @Param("toZipCodeId") Long toZipCodeId,
                                           @Param("customerId") Long customerId,
                                           @Param("headquartersId") Long headquartersId,
                                           @Param("vehicleTypeId") Integer vehicleTypeId,
                                           @Param("dateTime") LocalDateTime dateTime);
}
