package com.emonster.taroaichat.service;

import com.emonster.taroaichat.domain.TarotSession;
import com.emonster.taroaichat.repository.TarotSessionRepository;
import com.emonster.taroaichat.service.dto.TarotSessionDTO;
import com.emonster.taroaichat.service.mapper.TarotSessionMapper;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link com.emonster.taroaichat.domain.TarotSession}.
 */
@Service
@Transactional
public class TarotSessionService {

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

    private final TarotSessionRepository tarotSessionRepository;

    private final TarotSessionMapper tarotSessionMapper;

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

    /**
     * Save a tarotSession.
     *
     * @param tarotSessionDTO the entity to save.
     * @return the persisted entity.
     */
    public TarotSessionDTO save(TarotSessionDTO tarotSessionDTO) {
        LOG.debug("Request to save TarotSession : {}", tarotSessionDTO);
        TarotSession tarotSession = tarotSessionMapper.toEntity(tarotSessionDTO);
        tarotSession = tarotSessionRepository.save(tarotSession);
        return tarotSessionMapper.toDto(tarotSession);
    }

    /**
     * Update a tarotSession.
     *
     * @param tarotSessionDTO the entity to save.
     * @return the persisted entity.
     */
    public TarotSessionDTO update(TarotSessionDTO tarotSessionDTO) {
        LOG.debug("Request to update TarotSession : {}", tarotSessionDTO);
        TarotSession tarotSession = tarotSessionMapper.toEntity(tarotSessionDTO);
        tarotSession.setIsPersisted();
        tarotSession = tarotSessionRepository.save(tarotSession);
        return tarotSessionMapper.toDto(tarotSession);
    }

    /**
     * Partially update a tarotSession.
     *
     * @param tarotSessionDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<TarotSessionDTO> partialUpdate(TarotSessionDTO tarotSessionDTO) {
        LOG.debug("Request to partially update TarotSession : {}", tarotSessionDTO);

        return tarotSessionRepository
            .findById(tarotSessionDTO.getId())
            .map(existingTarotSession -> {
                tarotSessionMapper.partialUpdate(existingTarotSession, tarotSessionDTO);

                return existingTarotSession;
            })
            .map(tarotSessionRepository::save)
            .map(tarotSessionMapper::toDto);
    }

    /**
     * Get all the tarotSessions with eager load of many-to-many relationships.
     *
     * @return the list of entities.
     */
    public Page<TarotSessionDTO> findAllWithEagerRelationships(Pageable pageable) {
        return tarotSessionRepository.findAllWithEagerRelationships(pageable).map(tarotSessionMapper::toDto);
    }

    /**
     * Get one tarotSession by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<TarotSessionDTO> findOne(Long id) {
        LOG.debug("Request to get TarotSession : {}", id);
        return tarotSessionRepository.findOneWithEagerRelationships(id).map(tarotSessionMapper::toDto);
    }

    /**
     * Delete the tarotSession by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        LOG.debug("Request to delete TarotSession : {}", id);
        tarotSessionRepository.deleteById(id);
    }

    /**
     * Update feedback and rating for a session.
     *
     * @param sessionId the id of the session
     * @param feedback the feedback enum
     * @param rating the rating value
     * @param currentUserId the current user's id
     * @return the updated session DTO if user owns it
     */
    @Transactional
    public Optional<TarotSessionDTO> updateSessionFeedback(Long sessionId, com.emonster.taroaichat.domain.enumeration.Feedback feedback, Integer rating, Long currentUserId) {
        LOG.debug("Request to update feedback for TarotSession : {}", sessionId);
        
        return tarotSessionRepository.findOneWithEagerRelationships(sessionId)
            .filter(session -> {
                // Check if the session belongs to the current user
                return session.getUserProfile() != null && 
                       session.getUserProfile().getId() != null && 
                       session.getUserProfile().getId().equals(currentUserId);
            })
            .map(session -> {
                session.setFeedback(feedback);
                session.setRating(rating);
                return tarotSessionRepository.save(session);
            })
            .map(tarotSessionMapper::toDto);
    }
}
