package com.pressing.domain.discount;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
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.Optional;

@Repository
public interface DiscountRepository extends JpaRepository<Discount, Long>, JpaSpecificationExecutor<Discount> {

    Optional<Discount> findByIdAndSiteId(Long id, Long siteId);

    // Trouve les réductions valides aujourd'hui (pour un client spécifique OU globales)
    @Query("""
        SELECT d FROM Discount d 
        WHERE d.siteId = :siteId 
          AND (d.customerId = :customerId OR d.customerId IS NULL)
          AND :currentDate BETWEEN d.startDate AND d.endDate
    """)
    List<Discount> findActiveDiscountsForCustomer(
            @Param("siteId") Long siteId,
            @Param("customerId") Long customerId,
            @Param("currentDate") LocalDate currentDate
    );
}