package com.petshop.backend.repository;

import com.petshop.backend.entity.Sale;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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.util.List;

@Repository
public interface SaleRepository extends JpaRepository<Sale, Long> {

    @Query("SELECT s FROM Sale s WHERE " +
           "(:q IS NULL OR (" +
           "LOWER(s.employee.firstName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
           "LOWER(s.employee.lastName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
           "LOWER(s.store.storeName) LIKE LOWER(CONCAT('%', :q, '%'))" +
           ")) AND " +
           "(:paymentMethod IS NULL OR LOWER(s.paymentMethod) = LOWER(:paymentMethod)) AND " +
           "(:isRefund IS NULL OR s.isRefund = :isRefund) AND " +
           "(:storeId IS NULL OR s.store.storeId = :storeId)")
    Page<Sale> searchSales(@Param("q") String query, @Param("paymentMethod") String paymentMethod, @Param("storeId") Long storeId, @Param("isRefund") Boolean isRefund, Pageable pageable);

    List<Sale> findByOriginalSaleSaleId(Long originalSaleId);
}
