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.OutputData;
import com.oguerreiro.resilient.domain.OutputData_;
import com.oguerreiro.resilient.domain.Variable_;
import com.oguerreiro.resilient.repository.OutputDataRepository;
import com.oguerreiro.resilient.service.criteria.OutputDataCriteria;
import com.oguerreiro.resilient.service.dto.OutputDataDTO;
import com.oguerreiro.resilient.service.mapper.OutputDataMapper;

import jakarta.persistence.criteria.JoinType;

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

  public OutputDataQueryService(OutputDataRepository outputDataRepository, OutputDataMapper outputDataMapper) {
    super(outputDataRepository, outputDataMapper);
  }

  /**
   * Function to convert {@link OutputDataCriteria} 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<OutputData> createSpecification(OutputDataCriteria criteria) {
    Specification<OutputData> 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(), OutputData_.id));
      }
      if (criteria.getVariableId() != null) {
        specification = specification.and(buildSpecification(criteria.getVariableId(),
            root -> root.join(OutputData_.variable, JoinType.LEFT).get(ID_ATTRIBUTE_NAME)));
      }
      if (criteria.getVariableNameOrCode() != null) {
        // TODO : make a OR and search both Variable.code and Variable.name
        specification = specification.and(buildSpecification(criteria.getVariableNameOrCode(),
            root -> root.join(OutputData_.variable, JoinType.LEFT).get(Variable_.code)).or(
                buildSpecification(criteria.getVariableNameOrCode(),
                    root -> root.join(OutputData_.variable, JoinType.LEFT).get(Variable_.name))));
      }
      if (criteria.getVariableScopeId() != null) {
        specification = specification.and(buildSpecification(criteria.getVariableScopeId(),
            root -> root.join(OutputData_.variable, JoinType.LEFT).join(Variable_.variableScope, JoinType.LEFT).get(
                ID_ATTRIBUTE_NAME)));
      }
      if (criteria.getVariableCategoryId() != null) {
        specification = specification.and(buildSpecification(criteria.getVariableCategoryId(),
            root -> root.join(OutputData_.variable, JoinType.LEFT).join(Variable_.variableCategory, JoinType.LEFT).get(
                ID_ATTRIBUTE_NAME)));
      }
      if (criteria.getPeriodId() != null) {
        specification = specification.and(buildSpecification(criteria.getPeriodId(),
            root -> root.join(OutputData_.period, JoinType.LEFT).get(ID_ATTRIBUTE_NAME)));
      }
      if (criteria.getPeriodVersionId() != null) {
        specification = specification.and(buildSpecification(criteria.getPeriodVersionId(),
            root -> root.join(OutputData_.periodVersion, JoinType.LEFT).get(ID_ATTRIBUTE_NAME)));
      }
      if (criteria.getOwnerId() != null) {
        specification = specification.and(buildSpecification(criteria.getOwnerId(),
            root -> root.join(OutputData_.owner, JoinType.LEFT).get(ID_ATTRIBUTE_NAME)));
      }
    }
    return specification;
  }
}
