package com.pressing.domain.payment;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface PaymentRepository extends JpaRepository<Payment, Long>, JpaSpecificationExecutor<Payment> {
    Optional<Payment> findByIdAndSiteId(Long id, Long siteId);
    List<Payment> findBySiteIdAndPaymentDateTimeBetween(Long siteId, LocalDateTime start, LocalDateTime end);
    List<Payment> findByInvoiceIdOrderByPaymentDateTimeAsc(Long invoiceId);
    List<Payment> findByIroningInvoiceIdOrderByPaymentDateTimeAsc(Long ironingInvoiceId);

    // Charge invoice, ironingInvoice et customer en une seule requête (évite le N+1 sur la liste des règlements)
    @Override
    @EntityGraph(attributePaths = {"invoice", "ironingInvoice", "customer"})
    Page<Payment> findAll(Specification<Payment> spec, Pageable pageable);
}
