package com.emonster.taroaichat.service;

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

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

    private final ChatMessageRepository chatMessageRepository;

    private final ChatMessageMapper chatMessageMapper;

    public ChatMessageQueryService(ChatMessageRepository chatMessageRepository, ChatMessageMapper chatMessageMapper) {
        this.chatMessageRepository = chatMessageRepository;
        this.chatMessageMapper = chatMessageMapper;
    }

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

    /**
     * Function to convert {@link ChatMessageCriteria} 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<ChatMessage> createSpecification(ChatMessageCriteria criteria) {
        Specification<ChatMessage> 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(), ChatMessage_.id),
                buildSpecification(criteria.getSender(), ChatMessage_.sender),
                buildRangeSpecification(criteria.getTimestamp(), ChatMessage_.timestamp),
                buildStringSpecification(criteria.getCreatedBy(), ChatMessage_.createdBy),
                buildRangeSpecification(criteria.getCreatedDate(), ChatMessage_.createdDate),
                buildStringSpecification(criteria.getLastModifiedBy(), ChatMessage_.lastModifiedBy),
                buildRangeSpecification(criteria.getLastModifiedDate(), ChatMessage_.lastModifiedDate),
                buildSpecification(criteria.getSessionId(), root -> root.join(ChatMessage_.session, JoinType.LEFT).get(TarotSession_.id))
            );
        }
        return specification;
    }
}
