package de.votian.services.repository;

import de.votian.services.entity.ServiceType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface ServiceTypeRepository extends JpaRepository<ServiceType, Long> {

    Optional<ServiceType> findByNameAndModeAndHeadquartersId(String name, Integer mode, Long hqId);

    List<ServiceType> findByHeadquartersId(Long hqId);

    @Query("SELECT st FROM ServiceType st " +
           "WHERE (st.headquartersId = :hqId OR st.headquartersId = 0) " +
           "AND (COALESCE(st.customerId, 0) = 0 OR COALESCE(st.customerId, 0) = :customerId) " +
           "ORDER BY st.name")
    List<ServiceType> findAvailableForCustomer(@Param("hqId") Long hqId, @Param("customerId") Long customerId);
}
