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

import it.sw.pa.comune.artegna.domain.*; // for static metamodels
import it.sw.pa.comune.artegna.domain.Conferma;
import it.sw.pa.comune.artegna.repository.ConfermaRepository;
import it.sw.pa.comune.artegna.service.criteria.ConfermaCriteria;
import it.sw.pa.comune.artegna.service.dto.ConfermaDTO;
import it.sw.pa.comune.artegna.service.mapper.ConfermaMapper;
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 Conferma} entities in the database.
 * The main input is a {@link ConfermaCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link Page} of {@link ConfermaDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class ConfermaQueryService extends QueryService<Conferma> {

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

    private final ConfermaRepository confermaRepository;

    private final ConfermaMapper confermaMapper;

    public ConfermaQueryService(ConfermaRepository confermaRepository, ConfermaMapper confermaMapper) {
        this.confermaRepository = confermaRepository;
        this.confermaMapper = confermaMapper;
    }

    /**
     * Return a {@link Page} of {@link ConfermaDTO} 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<ConfermaDTO> findByCriteria(ConfermaCriteria criteria, Pageable page) {
        LOG.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<Conferma> specification = createSpecification(criteria);
        return confermaRepository.findAll(specification, page).map(confermaMapper::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(ConfermaCriteria criteria) {
        LOG.debug("count by criteria : {}", criteria);
        final Specification<Conferma> specification = createSpecification(criteria);
        return confermaRepository.count(specification);
    }

    /**
     * Function to convert {@link ConfermaCriteria} 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<Conferma> createSpecification(ConfermaCriteria criteria) {
        Specification<Conferma> 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(), Conferma_.id),
                buildStringSpecification(criteria.getMotivoConferma(), Conferma_.motivoConferma),
                buildSpecification(criteria.getTipoConferma(), Conferma_.tipoConferma),
                buildSpecification(criteria.getConfermataDaId(), root ->
                    root.join(Conferma_.confermataDa, JoinType.LEFT).get(UtenteApp_.id)
                ),
                buildSpecification(criteria.getPrenotazioneId(), root ->
                    root.join(Conferma_.prenotazione, JoinType.LEFT).get(Prenotazione_.id)
                )
            );
        }
        return specification;
    }
}
