package com.pablotj.ai.chat.application.usecase;

import com.pablotj.ai.chat.application.dto.ConversationSummaryDto;
import com.pablotj.ai.chat.application.mapper.ConversationMapper;
import com.pablotj.ai.chat.domain.repository.ConversationRepository;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Use case for retrieving conversation history.
 * Provides read-only access to conversation summaries for listing purposes.
 */
@Service
@Transactional(readOnly = true)
public class GetConversationHistoryUseCase {

    private static final Logger logger = LoggerFactory.getLogger(GetConversationHistoryUseCase.class);

    private final ConversationRepository conversationRepository;
    private final ConversationMapper conversationMapper;

    public GetConversationHistoryUseCase(
            ConversationRepository conversationRepository,
            ConversationMapper conversationMapper) {
        this.conversationRepository = conversationRepository;
        this.conversationMapper = conversationMapper;
    }

    /**
     * Retrieves all active conversations ordered by creation date.
     *
     * @return list of conversation summary DTOs
     */
    public List<ConversationSummaryDto> execute() {
        logger.debug("Retrieving conversation history");

        List<ConversationSummaryDto> conversations = conversationRepository
                .findAllActiveOrderedByCreationDate()
                .stream()
                .map(conversationMapper::toSummaryDto)
                .toList();

        logger.debug("Retrieved {} conversations", conversations.size());

        return conversations;
    }
}
