package com.petshop.backend.repository;

import com.petshop.backend.entity.Service;
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 ServiceRepository extends JpaRepository<Service, Long> {

    @Query("SELECT DISTINCT s FROM Service s LEFT JOIN s.species sp WHERE " +
           "(:q IS NULL OR LOWER(s.serviceName) LIKE LOWER(CONCAT('%', :q, '%')) OR LOWER(COALESCE(s.serviceDesc, '')) LIKE LOWER(CONCAT('%', :q, '%'))) AND " +
           "(:species IS NULL OR LOWER(sp) = LOWER(:species))")
    Page<Service> searchServices(@Param("q") String q, @Param("species") String species, Pageable pageable);
}
