package com.hpay.hpay_mobile_api.repositories;

import com.hpay.hpay_mobile_api.DTO.SochitelResponseDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.hpay.hpay_mobile_api.entities.Sochitel;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.time.LocalDateTime;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/*public interface SochitelRepository extends JpaRepository<Sochitel, Long> {

    @Query("SELECT s FROM Sochitel s " +
            "WHERE s.idClients = :idClient " +
            "AND (:idCompte IS NULL OR s.idCompte = :idCompte) " +
            "AND (:dateStart IS NULL OR s.dateEff >= :dateStart) " +
            "AND (:dateEnd IS NULL OR s.dateEff <= :dateEnd)")
    Page<Sochitel> findSochitelTransactions(
            @Param("idClient") Long idClient,
            @Param("idCompte") Long idCompte,
            @Param("dateStart") LocalDateTime dateStart,
            @Param("dateEnd") LocalDateTime dateEnd,
            Pageable pageable
    );
}*/



public interface SochitelRepository extends JpaRepository<Sochitel, Long> {

    @Query("""
        SELECT new com.hpay.hpay_mobile_api.DTO.SochitelResponseDto(
            s.id, s.montant, s.frais, s.total, s.reference, s.dateEff, s.statut,
            c.id, c.clientCode, c.nom, c.prenoms, c.telephone, c.email, c.photoClient,
            cp.idCompte, cp.numCompte, cp.devise
        )
        FROM Sochitel s
        JOIN s.client c
        JOIN s.compte cp
        WHERE (:idClient IS NULL OR s.idClients = :idClient)
          AND (:idCompte IS NULL OR s.idCompte = :idCompte)
          AND (:dateStart IS NULL OR s.dateEff >= :dateStart)
          AND (:dateEnd IS NULL OR s.dateEff <= :dateEnd)
        """)
    Page<SochitelResponseDto> searchTransactions(
            @Param("idClient") Integer idClient,
            @Param("idCompte") Integer idCompte,
            @Param("dateStart") LocalDateTime dateStart,
            @Param("dateEnd") LocalDateTime dateEnd,
            Pageable pageable
    );
}
