package tech.easyflow.ai.service;

import com.easyagents.core.message.Message;
import com.easyagents.core.message.UserMessage;
import com.easyagents.core.model.chat.ChatModel;
import com.easyagents.core.model.chat.ChatOptions;
import com.easyagents.core.prompt.MemoryPrompt;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import tech.easyflow.ai.entity.Bot;
import com.mybatisflex.core.service.IService;
import tech.easyflow.ai.service.impl.BotServiceImpl;
import tech.easyflow.core.chat.protocol.sse.ChatSseEmitter;
import tech.easyflow.core.runtime.ChatRuntimeContext;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 *  服务层。
 *
 * @author michael
 * @since 2024-08-23
 */
public interface BotService extends IService<Bot> {

    Bot getDetail(String id);

    void updateBotLlmId(Bot aiBot);

    Bot getByAlias(String alias);

    /**
     * 获取已发布视图。
     *
     * @param idOrAlias ID 或别名
     * @return 已发布视图
     */
    Bot getPublishedDetail(String idOrAlias);

    /**
     * 根据 ID 获取已发布视图。
     *
     * @param id 机器人 ID
     * @return 已发布视图
     */
    Bot getPublishedById(BigInteger id);

    /**
     * 把当前资源映射为已发布视图。
     *
     * @param bot 当前资源
     * @return 已发布视图
     */
    Bot toPublishedView(Bot bot);

    SseEmitter checkChatBeforeStart(BigInteger botId, String prompt, String conversationId, BotServiceImpl.ChatCheckResult chatCheckResult);

    SseEmitter checkChatBeforeStart(BigInteger botId, String prompt, String conversationId,
                                    BotServiceImpl.ChatCheckResult chatCheckResult, BigInteger regenerateRoundId);

    /**
     * 聊天前置校验，并根据调用方要求决定是否强制走发布态。
     *
     * @param botId 聊天助手 ID
     * @param prompt 用户问题
     * @param conversationId 会话 ID
     * @param chatCheckResult 校验结果承载对象
     * @param publishedOnly 是否强制走发布态
     * @return 校验失败时返回错误 SSE；成功时返回 {@code null}
     */
    SseEmitter checkChatBeforeStart(BigInteger botId, String prompt, String conversationId,
                                    BotServiceImpl.ChatCheckResult chatCheckResult, boolean publishedOnly);

    SseEmitter checkChatBeforeStart(BigInteger botId, String prompt, String conversationId,
                                    BotServiceImpl.ChatCheckResult chatCheckResult, boolean publishedOnly, BigInteger regenerateRoundId);

    SseEmitter startChat(BigInteger botId, String prompt, BigInteger conversationId, List<Map<String, String>> messages,
                         BotServiceImpl.ChatCheckResult chatCheckResult, List<String> attachments, ChatRuntimeContext runtimeContext);

    SseEmitter startPublicChat(BigInteger botId, String prompt, List<Message> messages,
                               BotServiceImpl.ChatCheckResult chatCheckResult, ChatRuntimeContext runtimeContext);

}
