package tech.easyflow.ai.entity;

import com.easyagents.core.message.AiMessage;
import com.easyagents.core.message.ToolCall;
import com.easyagents.core.message.ToolMessage;
import com.easyagents.core.model.client.OpenAIChatMessageSerializer;
import com.easyagents.llm.deepseek.DeepseekChatModel;
import com.easyagents.llm.deepseek.DeepseekConfig;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;
import java.util.Map;

/**
 * DeepSeek thinking/tool 配置测试。
 */
public class ModelDeepseekConfigTest {

    /**
     * DeepSeek 聊天模型应显式开启 thinking/tool 相关协议开关。
     */
    @Test
    public void shouldEnableDeepseekThinkingProtocolFlags() {
        ModelProvider provider = new ModelProvider();
        provider.setProviderType("deepseek");
        provider.setProviderName("deepseek");

        Model model = new Model();
        model.setModelProvider(provider);
        model.setEndpoint("https://api.deepseek.com");
        model.setApiKey("sk-test");
        model.setModelName("deepseek-chat");
        model.setRequestPath("/chat/completions");

        DeepseekChatModel chatModel = (DeepseekChatModel) model.toChatModel();
        DeepseekConfig config = chatModel.getConfig();

        Assert.assertTrue(config.isSupportThinking());
        Assert.assertEquals("deepseek", config.getThinkingProtocol());
        Assert.assertTrue(config.isNeedReasoningContentForToolMessage());
    }

    /**
     * DeepSeek 在 tool call assistant 历史上应序列化 reasoning_content。
     */
    @Test
    public void shouldSerializeReasoningContentForToolAssistantMessage() {
        DeepseekConfig config = new DeepseekConfig();
        config.setNeedReasoningContentForToolMessage(Boolean.TRUE);
        config.setThinkingProtocol("deepseek");
        config.setSupportThinking(Boolean.TRUE);

        AiMessage assistant = new AiMessage(null);
        assistant.setReasoningContent("先推理");
        assistant.setToolCalls(List.of(new ToolCall("call-1", "kb_search", "{\"query\":\"java\"}")));

        ToolMessage toolMessage = new ToolMessage();
        toolMessage.setToolCallId("call-1");
        toolMessage.setContent("{\"hits\":1}");

        List<Map<String, Object>> serialized = new OpenAIChatMessageSerializer().serializeMessages(
                List.of(assistant, toolMessage),
                config
        );

        Assert.assertEquals(2, serialized.size());
        Assert.assertEquals("assistant", serialized.get(0).get("role"));
        Assert.assertEquals("先推理", serialized.get(0).get("reasoning_content"));
        Assert.assertTrue(serialized.get(0).containsKey("tool_calls"));
        Assert.assertEquals("tool", serialized.get(1).get("role"));
        Assert.assertEquals("call-1", serialized.get(1).get("tool_call_id"));
    }
}
