package br.gov.sigefp.rh.repository;

import br.gov.sigefp.rh.domain.TaxBracket;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

@Repository
public interface TaxBracketRepository extends JpaRepository<TaxBracket, UUID> {

    @Query("SELECT b FROM TaxBracket b WHERE b.validFrom <= :date AND (b.validTo IS NULL OR b.validTo >= :date) ORDER BY b.lowerLimit ASC")
    List<TaxBracket> findActiveBrackets(@Param("date") LocalDate date);

    @Query("SELECT b FROM TaxBracket b WHERE b.deductionType.id = :typeId AND (b.validTo IS NULL OR b.validTo >= :start) AND b.validFrom <= :end "
            +
            "AND b.lowerLimit < :upper AND (b.upperLimit IS NULL OR b.upperLimit > :lower)")
    List<TaxBracket> findOverlappingBrackets(@Param("typeId") UUID typeId,
            @Param("start") LocalDate start, @Param("end") LocalDate end,
            @Param("lower") java.math.BigDecimal lower, @Param("upper") java.math.BigDecimal upper);
}
