package tech.easyflow.chatlog.service.impl;

import org.springframework.stereotype.Service;
import tech.easyflow.chatlog.domain.dto.ChatHistoryPage;
import tech.easyflow.chatlog.domain.query.ChatPageQuery;
import tech.easyflow.chatlog.repository.analyticaldb.ChatAnalyticalDBRepository;
import tech.easyflow.chatlog.service.ChatHistoryQueryService;
import tech.easyflow.chatlog.service.ChatSessionExtensionDispatcher;
import tech.easyflow.chatlog.service.ChatSessionQueryService;

import java.math.BigInteger;

@Service
public class ChatHistoryQueryServiceImpl implements ChatHistoryQueryService {

    private final ChatAnalyticalDBRepository chatAnalyticalDBRepository;
    private final ChatSessionQueryService chatSessionQueryService;
    private final ChatSessionExtensionDispatcher extensionDispatcher;

    /**
     * 创建归档历史查询服务。
     *
     * @param chatAnalyticalDBRepository 分析库仓储
     * @param chatSessionQueryService 会话摘要查询服务
     * @param extensionDispatcher 会话业务扩展分发器
     */
    public ChatHistoryQueryServiceImpl(ChatAnalyticalDBRepository chatAnalyticalDBRepository,
                                       ChatSessionQueryService chatSessionQueryService,
                                       ChatSessionExtensionDispatcher extensionDispatcher) {
        this.chatAnalyticalDBRepository = chatAnalyticalDBRepository;
        this.chatSessionQueryService = chatSessionQueryService;
        this.extensionDispatcher = extensionDispatcher;
    }

    @Override
    public ChatHistoryPage queryHistoryMessages(BigInteger sessionId, ChatPageQuery query) {
        ChatHistoryPage page = chatAnalyticalDBRepository.queryHistory(sessionId, query);
        extensionDispatcher.projectMessages(
                chatSessionQueryService.getSessionSummary(sessionId), page.getRecords());
        return page;
    }
}
