package com.oguerreiro.resilient.service.query;

import java.util.List;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

// for static metamodels
import com.oguerreiro.resilient.domain.UnitConverter;
import com.oguerreiro.resilient.domain.UnitConverter_;
import com.oguerreiro.resilient.repository.UnitConverterRepository;
import com.oguerreiro.resilient.service.criteria.UnitConverterCriteria;
import com.oguerreiro.resilient.service.dto.UnitConverterDTO;
import com.oguerreiro.resilient.service.mapper.UnitConverterMapper;

import jakarta.persistence.criteria.JoinType;

/**
 * Service for executing complex queries for {@link UnitConverter} entities in the
 * database. The main input is a {@link UnitConverterCriteria} which gets converted
 * to {@link Specification}, in a way that all the filters must apply. It
 * returns a {@link List} of {@link UnitConverterDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class UnitConverterQueryService
    extends AbstractQueryService<UnitConverter, UnitConverterDTO, Long, UnitConverterCriteria> {

  public UnitConverterQueryService(UnitConverterRepository unitConverterRepository,
      UnitConverterMapper unitConverterMapper) {
    super(unitConverterRepository, unitConverterMapper);
  }

  /**
   * Function to convert {@link UnitConverterCriteria} to a {@link Specification}
   * 
   * @param  criteria The object which holds all the filters, which the entities
   *                  should match.
   * @return          the matching {@link Specification} of the entity.
   */
  protected Specification<UnitConverter> createSpecification(UnitConverterCriteria criteria) {
    Specification<UnitConverter> specification = Specification.where(null);
    if (criteria != null) {
      // This has to be called first, because the distinct method returns null
      if (criteria.getDistinct() != null) {
        specification = specification.and(distinct(criteria.getDistinct()));
      }
      if (criteria.getId() != null) {
        specification = specification.and(buildRangeSpecification(criteria.getId(), UnitConverter_.id));
      }
      if (criteria.getDescription() != null) {
        specification = specification.and(
            buildStringSpecification(criteria.getDescription(), UnitConverter_.description));
      }
      if (criteria.getName() != null) {
        specification = specification.and(buildStringSpecification(criteria.getName(), UnitConverter_.name));
      }
      if (criteria.getFromUnitId() != null) {
        specification = specification.and(buildSpecification(criteria.getFromUnitId(),
            root -> root.join(UnitConverter_.fromUnit, JoinType.LEFT).get(ID_ATTRIBUTE_NAME)));
      }
      if (criteria.getToUnitId() != null) {
        specification = specification.and(buildSpecification(criteria.getToUnitId(),
            root -> root.join(UnitConverter_.toUnit, JoinType.LEFT).get(ID_ATTRIBUTE_NAME)));
      }

    }
    return specification;
  }
}
