package it.sw.pa.comune.artegna.service;

import it.sw.pa.comune.artegna.domain.*; // for static metamodels
import it.sw.pa.comune.artegna.domain.Struttura;
import it.sw.pa.comune.artegna.repository.StrutturaRepository;
import it.sw.pa.comune.artegna.service.criteria.StrutturaCriteria;
import it.sw.pa.comune.artegna.service.dto.StrutturaDTO;
import it.sw.pa.comune.artegna.service.mapper.StrutturaMapper;
import jakarta.persistence.criteria.JoinType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.jhipster.service.QueryService;

/**
 * Service for executing complex queries for {@link Struttura} entities in the database.
 * The main input is a {@link StrutturaCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link Page} of {@link StrutturaDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class StrutturaQueryService extends QueryService<Struttura> {

    private static final Logger LOG = LoggerFactory.getLogger(StrutturaQueryService.class);

    private final StrutturaRepository strutturaRepository;

    private final StrutturaMapper strutturaMapper;

    public StrutturaQueryService(StrutturaRepository strutturaRepository, StrutturaMapper strutturaMapper) {
        this.strutturaRepository = strutturaRepository;
        this.strutturaMapper = strutturaMapper;
    }

    /**
     * Return a {@link Page} of {@link StrutturaDTO} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<StrutturaDTO> findByCriteria(StrutturaCriteria criteria, Pageable page) {
        LOG.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<Struttura> specification = createSpecification(criteria);
        return strutturaRepository.findAll(specification, page).map(strutturaMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(StrutturaCriteria criteria) {
        LOG.debug("count by criteria : {}", criteria);
        final Specification<Struttura> specification = createSpecification(criteria);
        return strutturaRepository.count(specification);
    }

    /**
     * Function to convert {@link StrutturaCriteria} 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<Struttura> createSpecification(StrutturaCriteria criteria) {
        Specification<Struttura> specification = Specification.unrestricted();
        if (criteria != null) {
            // This has to be called first, because the distinct method returns null
            specification = Specification.allOf(
                Boolean.TRUE.equals(criteria.getDistinct()) ? distinct(criteria.getDistinct()) : null,
                buildRangeSpecification(criteria.getId(), Struttura_.id),
                buildStringSpecification(criteria.getNome(), Struttura_.nome),
                buildStringSpecification(criteria.getDescrizione(), Struttura_.descrizione),
                buildStringSpecification(criteria.getIndirizzo(), Struttura_.indirizzo),
                buildRangeSpecification(criteria.getCapienzaMax(), Struttura_.capienzaMax),
                buildSpecification(criteria.getAttiva(), Struttura_.attiva),
                buildStringSpecification(criteria.getFotoUrl(), Struttura_.fotoUrl),
                buildRangeSpecification(criteria.getCreatedAt(), Struttura_.createdAt),
                buildRangeSpecification(criteria.getUpdatedAt(), Struttura_.updatedAt),
                buildSpecification(criteria.getDisponibilitaId(), root ->
                    root.join(Struttura_.disponibilitas, JoinType.LEFT).get(Disponibilita_.id)
                ),
                buildSpecification(criteria.getModuliLiberatorieId(), root ->
                    root.join(Struttura_.moduliLiberatories, JoinType.LEFT).get(ModelloLiberatoria_.id)
                )
            );
        }
        return specification;
    }
}
