package com.emonster.taroaichat.service;

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

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

    private final TarotSessionRepository tarotSessionRepository;

    private final TarotSessionMapper tarotSessionMapper;

    public TarotSessionQueryService(TarotSessionRepository tarotSessionRepository, TarotSessionMapper tarotSessionMapper) {
        this.tarotSessionRepository = tarotSessionRepository;
        this.tarotSessionMapper = tarotSessionMapper;
    }

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

    /**
     * Function to convert {@link TarotSessionCriteria} 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<TarotSession> createSpecification(TarotSessionCriteria criteria) {
        Specification<TarotSession> 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(), TarotSession_.id),
                buildSpecification(criteria.getStatus(), TarotSession_.status),
                buildSpecification(criteria.getFeedback(), TarotSession_.feedback),
                buildRangeSpecification(criteria.getRating(), TarotSession_.rating),
                buildSpecification(criteria.getSaved(), TarotSession_.saved),
                buildStringSpecification(criteria.getScreenshotUrl(), TarotSession_.screenshotUrl),
                buildRangeSpecification(criteria.getCompletedAt(), TarotSession_.completedAt),
                buildStringSpecification(criteria.getCreatedBy(), TarotSession_.createdBy),
                buildRangeSpecification(criteria.getCreatedDate(), TarotSession_.createdDate),
                buildStringSpecification(criteria.getLastModifiedBy(), TarotSession_.lastModifiedBy),
                buildRangeSpecification(criteria.getLastModifiedDate(), TarotSession_.lastModifiedDate),
                buildSpecification(criteria.getChatMessageId(), root ->
                    root.join(TarotSession_.chatMessages, JoinType.LEFT).get(ChatMessage_.id)
                ),
                buildSpecification(criteria.getUserProfileId(), root ->
                    root.join(TarotSession_.userProfile, JoinType.LEFT).get(UserProfile_.id)
                ),
                buildSpecification(criteria.getUserProfilePhone(), root ->
                    root.join(TarotSession_.userProfile, JoinType.LEFT).get(UserProfile_.phone)
                )
            );
        }
        return specification;
    }
}
