package de.votian.services.repository;

import de.votian.services.entity.ServiceEntity;
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 ServiceEntityRepository extends JpaRepository<ServiceEntity, Long> {

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

    List<ServiceEntity> findByHeadquartersId(Long hqId);

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