package de.votian.services.repository;

import de.votian.services.entity.ServiceZipCodeAreaHistory;
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 ServiceZipCodeAreaHistoryRepository extends JpaRepository<ServiceZipCodeAreaHistory, Long> {

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