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.UnitConverter;

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

  @Override
  @Query("select unitConverter from UnitConverter unitConverter left join fetch unitConverter.fromUnit left join fetch unitConverter.toUnit")
  List<UnitConverter> findAllWithEagerRelationships();

  @Override
  @Query("select unitConverter from UnitConverter unitConverter left join fetch unitConverter.fromUnit left join fetch unitConverter.toUnit where unitConverter.id =:id")
  Optional<UnitConverter> findOneWithEagerRelationships(@Param("id") Long id);

  /* Custom EmissionFactorRepository methods */
  /* *************************************** */
  @Query("select unitConverter from UnitConverter unitConverter where unitConverter.fromUnit.id =:fromUnitId and unitConverter.toUnit.id =:toUnitId")
  List<UnitConverter> findAllFromAndTo(@Param("fromUnitId") Long fromUnitId, @Param("toUnitId") Long toUnitId);
}
