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

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

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

    private final NotificaRepository notificaRepository;

    private final NotificaMapper notificaMapper;

    public NotificaQueryService(NotificaRepository notificaRepository, NotificaMapper notificaMapper) {
        this.notificaRepository = notificaRepository;
        this.notificaMapper = notificaMapper;
    }

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

    /**
     * Function to convert {@link NotificaCriteria} 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<Notifica> createSpecification(NotificaCriteria criteria) {
        Specification<Notifica> 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(), Notifica_.id),
                buildSpecification(criteria.getTipoCanale(), Notifica_.tipoCanale),
                buildSpecification(criteria.getTipoEvento(), Notifica_.tipoEvento),
                buildStringSpecification(criteria.getMessaggio(), Notifica_.messaggio),
                buildSpecification(criteria.getInviata(), Notifica_.inviata),
                buildRangeSpecification(criteria.getInviataAt(), Notifica_.inviataAt),
                buildStringSpecification(criteria.getErrore(), Notifica_.errore),
                buildRangeSpecification(criteria.getCreatedAt(), Notifica_.createdAt),
                buildSpecification(criteria.getConfermaId(), root -> root.join(Notifica_.conferma, JoinType.LEFT).get(Conferma_.id))
            );
        }
        return specification;
    }
}
