package de.votian.services.repository;

import de.votian.services.entity.AppGroup;
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;

public interface AppGroupRepository extends JpaRepository<AppGroup, Long> {

    List<AppGroup> findByMandatorIdOrderByNameAsc(Long mandatorId);

    @Query("SELECT g FROM AppGroup g " +
           "WHERE g.mandatorId = :mandatorId " +
           "AND (g.headquartersId = :hqId OR g.headquartersId = 0) " +
           "ORDER BY LOWER(g.name), g.id")
    List<AppGroup> findVisibleForHeadquarters(@Param("mandatorId") Long mandatorId,
                                              @Param("hqId") Long hqId);

    @Query("SELECT g FROM AppGroup g " +
           "WHERE g.headquartersId = :hqId OR g.headquartersId = 0 " +
           "ORDER BY LOWER(g.name), g.id")
    List<AppGroup> findVisibleForHeadquarters(@Param("hqId") Long hqId);
}
