package de.votian.services.repository;

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

    @Query("SELECT sph FROM ServicePriceHistory sph WHERE sph.objectType = :objectType " +
           "AND sph.objectId = :objectId AND sph.fieldName = :fieldName AND sph.validFrom <= :validFrom " +
           "ORDER BY sph.validFrom DESC")
    List<ServicePriceHistory> findLatestFieldValue(@Param("objectType") String objectType,
                                                   @Param("objectId") Long objectId,
                                                   @Param("fieldName") String fieldName,
                                                   @Param("validFrom") LocalDateTime validFrom);

    @Query("SELECT sph FROM ServicePriceHistory sph WHERE sph.objectType = :objectType " +
           "AND sph.objectId = :objectId AND sph.fieldName = :fieldName AND sph.context = :context " +
           "AND sph.validFrom <= :validFrom ORDER BY sph.validFrom DESC")
    List<ServicePriceHistory> findLatestFieldValueWithContext(@Param("objectType") String objectType,
                                                              @Param("objectId") Long objectId,
                                                              @Param("fieldName") String fieldName,
                                                              @Param("context") String context,
                                                              @Param("validFrom") LocalDateTime validFrom);
}
