package de.votian.services.repository;

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

    List<CostCenter> findByCustomerId(Long customerId);

    Optional<CostCenter> findByCustomerIdAndName(Long customerId, String name);

    Optional<CostCenter> findFirstByCustomerIdAndParentIdIsNull(Long customerId);

    @Query("SELECT csc FROM CostCenter csc WHERE csc.customerId = :csId AND csc.visible = '1'")
    List<CostCenter> findVisibleByCustomerId(@Param("csId") Long csId);

    @Query("SELECT csc FROM CostCenter csc WHERE csc.path LIKE %:pathFragment% AND csc.customerId = :csId")
    List<CostCenter> findByPathContainingAndCustomerId(@Param("pathFragment") String pathFragment, @Param("csId") Long csId);
}
