package com.petshop.backend.repository;

import com.petshop.backend.entity.ProductSupplier;
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;

@Repository
public interface ProductSupplierRepository extends JpaRepository<ProductSupplier, ProductSupplier.ProductSupplierId> {

    @Query("SELECT ps FROM ProductSupplier ps WHERE " +
           "(:q IS NULL OR (LOWER(ps.product.prodName) LIKE LOWER(CONCAT('%', :q, '%')) OR " +
           "LOWER(ps.supplier.supCompany) LIKE LOWER(CONCAT('%', :q, '%')))) AND " +
           "(:productId IS NULL OR ps.product.prodId = :productId) AND " +
           "(:supplierId IS NULL OR ps.supplier.supId = :supplierId)")
    Page<ProductSupplier> searchProductSuppliers(
            @Param("q") String query,
            @Param("productId") Long productId,
            @Param("supplierId") Long supplierId,
            Pageable pageable);
}
