package com.emonster.taroaichat.service;

import com.emonster.taroaichat.domain.*; // for static metamodels
import com.emonster.taroaichat.domain.TarotCard;
import com.emonster.taroaichat.repository.TarotCardRepository;
import com.emonster.taroaichat.service.criteria.TarotCardCriteria;
import com.emonster.taroaichat.service.dto.TarotCardDTO;
import com.emonster.taroaichat.service.mapper.TarotCardMapper;
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 TarotCard} entities in the database.
 * The main input is a {@link TarotCardCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link Page} of {@link TarotCardDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class TarotCardQueryService extends QueryService<TarotCard> {

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

    private final TarotCardRepository tarotCardRepository;

    private final TarotCardMapper tarotCardMapper;

    public TarotCardQueryService(TarotCardRepository tarotCardRepository, TarotCardMapper tarotCardMapper) {
        this.tarotCardRepository = tarotCardRepository;
        this.tarotCardMapper = tarotCardMapper;
    }

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

    /**
     * Function to convert {@link TarotCardCriteria} 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<TarotCard> createSpecification(TarotCardCriteria criteria) {
        Specification<TarotCard> specification = Specification.where(null);
        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(), TarotCard_.id),
                buildStringSpecification(criteria.getName(), TarotCard_.name),
                buildSpecification(criteria.getArcanaType(), TarotCard_.arcanaType),
                buildRangeSpecification(criteria.getCardNumber(), TarotCard_.cardNumber),
                buildStringSpecification(criteria.getImageUrl(), TarotCard_.imageUrl),
                buildStringSpecification(criteria.getKeywords(), TarotCard_.keywords),
                buildStringSpecification(criteria.getCreatedBy(), TarotCard_.createdBy),
                buildRangeSpecification(criteria.getCreatedDate(), TarotCard_.createdDate),
                buildStringSpecification(criteria.getLastModifiedBy(), TarotCard_.lastModifiedBy),
                buildRangeSpecification(criteria.getLastModifiedDate(), TarotCard_.lastModifiedDate)
            );
        }
        return specification;
    }
}
