package tech.easyflow.chatlog.service.impl;

import org.springframework.stereotype.Service;
import tech.easyflow.chatlog.domain.command.ChatSessionUpsertCommand;
import tech.easyflow.chatlog.domain.dto.ChatSessionSummary;
import tech.easyflow.chatlog.service.ChatPersistDispatcher;
import tech.easyflow.chatlog.service.ChatSessionCommandService;
import tech.easyflow.chatlog.service.ChatSessionExtensionDispatcher;
import tech.easyflow.chatlog.service.ChatSessionQueryService;

import java.math.BigInteger;

@Service
public class ChatSessionCommandServiceImpl implements ChatSessionCommandService {

    private final ChatPersistDispatcher chatPersistDispatcher;
    private final ChatSessionQueryService chatSessionQueryService;
    private final ChatSessionExtensionDispatcher extensionDispatcher;

    /**
     * 创建会话命令服务。
     *
     * @param chatPersistDispatcher 聊天持久化分发器
     * @param chatSessionQueryService 会话查询服务
     * @param extensionDispatcher 会话业务扩展分发器
     */
    public ChatSessionCommandServiceImpl(ChatPersistDispatcher chatPersistDispatcher,
                                         ChatSessionQueryService chatSessionQueryService,
                                         ChatSessionExtensionDispatcher extensionDispatcher) {
        this.chatPersistDispatcher = chatPersistDispatcher;
        this.chatSessionQueryService = chatSessionQueryService;
        this.extensionDispatcher = extensionDispatcher;
    }

    @Override
    public ChatSessionSummary createOrTouchSession(ChatSessionUpsertCommand command) {
        return chatPersistDispatcher.createOrTouchSession(command);
    }

    @Override
    public void renameSession(BigInteger sessionId, BigInteger userId, String title, BigInteger operatorId) {
        chatPersistDispatcher.renameSession(sessionId, userId, title, operatorId);
    }

    @Override
    public void deleteSession(BigInteger sessionId, BigInteger userId, BigInteger operatorId) {
        ChatSessionSummary summary = chatSessionQueryService.getSessionSummary(sessionId);
        extensionDispatcher.beforeDelete(summary, userId, operatorId);
        chatPersistDispatcher.deleteSession(sessionId, userId, operatorId);
        extensionDispatcher.afterDelete(summary, userId, operatorId);
    }
}
