package com.oguerreiro.resilient.repository;

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

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.oguerreiro.resilient.domain.VariableCategory;

/**
 * Spring Data JPA repository for the VariableCategory entity.
 */
@Repository
public interface VariableCategoryRepository extends ResilientJpaRepository<VariableCategory, Long> {
  /* Interface ResilientJpaRepository eager fetching Override methods */
  /* **************************************************************** */
  @Override
  @Query(value = "select variableCategory from VariableCategory variableCategory left join fetch variableCategory.variableScope", countQuery = "select count(variableCategory) from VariableCategory variableCategory")
  Page<VariableCategory> findAllWithEagerRelationships(Pageable pageable);

  @Override
  @Query("select variableCategory from VariableCategory variableCategory left join fetch variableCategory.variableScope")
  List<VariableCategory> findAllWithEagerRelationships();

  @Override
  @Query("select variableCategory from VariableCategory variableCategory left join fetch variableCategory.variableScope where variableCategory.id =:id")
  Optional<VariableCategory> findOneWithEagerRelationships(@Param("id") Long id);

  /* Custom VariableCategoryRepository methods */
  /* ***************************************** */
  @Query("select cat from VariableCategory cat left join fetch cat.variableScope scope where scope.id=:scopeId order by cat.name")
  List<VariableCategory> findAllByScopeWithToOneRelationships(@Param("scopeId") Long scopeId);

  @Query("select cat from VariableCategory cat left join fetch cat.variableScope scope where scope.id=:scopeId AND (NOT(cat.hiddenForFactor) OR cat.hiddenForFactor IS NULL) order by cat.name")
  List<VariableCategory> findByVariableScopeIdAndHiddenForFactorIsFalseOrHiddenForFactorIsNullOrderByNameAsc(
      Long scopeId);
}
