package com.emonster.taroaichat.service;

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

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

    private final DonationRepository donationRepository;

    private final DonationMapper donationMapper;

    public DonationQueryService(DonationRepository donationRepository, DonationMapper donationMapper) {
        this.donationRepository = donationRepository;
        this.donationMapper = donationMapper;
    }

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

    /**
     * Function to convert {@link DonationCriteria} 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<Donation> createSpecification(DonationCriteria criteria) {
        Specification<Donation> 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(), Donation_.id),
                buildRangeSpecification(criteria.getAmount(), Donation_.amount),
                buildStringSpecification(criteria.getCurrency(), Donation_.currency),
                buildStringSpecification(criteria.getStripePaymentIntentId(), Donation_.stripePaymentIntentId),
                buildSpecification(criteria.getStatus(), Donation_.status),
                buildStringSpecification(criteria.getCreatedBy(), Donation_.createdBy),
                buildRangeSpecification(criteria.getCreatedDate(), Donation_.createdDate),
                buildStringSpecification(criteria.getLastModifiedBy(), Donation_.lastModifiedBy),
                buildRangeSpecification(criteria.getLastModifiedDate(), Donation_.lastModifiedDate),
                buildSpecification(criteria.getSessionId(), root -> root.join(Donation_.session, JoinType.LEFT).get(TarotSession_.id)),
                buildSpecification(criteria.getUserProfileId(), root -> root.join(Donation_.userProfile, JoinType.LEFT).get(UserProfile_.id)),
                buildSpecification(criteria.getUserProfilePhone(), root ->
                    root.join(Donation_.userProfile, JoinType.LEFT).get(UserProfile_.phone)
                )
            );
        }
        return specification;
    }
}
