package br.gov.sigefp.rh.repository;

import br.gov.sigefp.rh.domain.GlobalDeductionRule;
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 GlobalDeductionRuleRepository extends JpaRepository<GlobalDeductionRule, UUID> {

    @Query("SELECT r FROM GlobalDeductionRule r WHERE r.active = true AND r.validFrom <= :date AND (r.validTo IS NULL OR r.validTo >= :date)")
    List<GlobalDeductionRule> findActiveRules(@Param("date") LocalDate date);

    @Query("SELECT r FROM GlobalDeductionRule r WHERE r.deductionType.id = :typeId AND r.active = true AND (r.validTo IS NULL OR r.validTo >= :start) AND r.validFrom <= :end")
    List<GlobalDeductionRule> findOverlappingRules(@Param("typeId") UUID typeId, @Param("start") LocalDate start,
            @Param("end") LocalDate end);
}
