package com.jeelowcode.framework.ai;

import com.jeelowcode.framework.ai.config.DeepSeekConfig;
import com.jeelowcode.framework.ai.config.KimiConfig;
import com.jeelowcode.framework.ai.platform.chat.ChatBaseRequest;
import com.jeelowcode.framework.ai.platform.chat.ChatBaseResponse;
import com.jeelowcode.framework.ai.platform.chat.tool.ChatMessage;
import com.jeelowcode.framework.ai.platform.chat.tool.Choice;
import com.jeelowcode.framework.ai.service.Configuration;
import com.jeelowcode.framework.ai.service.IChatService;
import com.jeelowcode.framework.ai.service.PlatformType;
import com.jeelowcode.framework.ai.service.factor.AiService;
import com.jeelowcode.framework.ai.template.CreateTableTemplate;
import com.jeelowcode.framework.utils.utils.FuncBase;
import lombok.Data;
import okhttp3.OkHttpClient;
import org.junit.Before;
import org.junit.Test;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TestUtils {
    private IChatService chatService;

    @Before
    public void test_init() throws NoSuchAlgorithmException, KeyManagementException {
        KimiConfig kimiConfig = new KimiConfig();
        kimiConfig.setApiKey("sk-EE98wvDeTFvxRcBxDQmWhKQlewkGFIrfyCxvyNxDj8Vaf701");


        DeepSeekConfig deepseekConfig = new DeepSeekConfig();
        deepseekConfig.setApiKey("sk-b31fcf4e27bf45fbb65ace5bbc627d5d");

        Configuration configuration = new Configuration();
        configuration.setKimiConfig(kimiConfig);
        configuration.setDeepSeekConfig(deepseekConfig);


        OkHttpClient okHttpClient = new OkHttpClient
                .Builder()
                .connectTimeout(300, TimeUnit.SECONDS)
                .writeTimeout(300, TimeUnit.SECONDS)
                .readTimeout(300, TimeUnit.SECONDS)
                .build();
        configuration.setOkHttpClient(okHttpClient);

        AiService aiService = new AiService(configuration);

        chatService = aiService.getChatService(PlatformType.KIMI);
        //chatService = aiService.getChatService(PlatformType.DEEPSEEK);
    }



    @Test
    public void test_chatCompletions_common() throws Exception {

        long time1 = System.currentTimeMillis();

        ChatBaseRequest chatBaseRequest = ChatBaseRequest.builder()
                //.model("deepseek-chat")
                .messages(CreateTableTemplate.getMessageTemplateList())
                .message(ChatMessage.withUser("物流信息，其中要有，物流单号，收件人，寄件人"))
                .build();


        ChatBaseResponse chatBaseResponse = chatService.chatCompletion(chatBaseRequest);
        List<Choice> choices = chatBaseResponse.getChoices();
        if(FuncBase.isEmpty(choices)){
            return;
        }
        Choice choice = choices.get(0);
        ChatMessage message = choice.getMessage();
        if(FuncBase.isEmpty(message)){
            return;
        }
        String content = message.getContent();
        System.out.println("content===="+content);
        CreateTableTemplate.RspModel rspModel = FuncBase.json2Bean(content, CreateTableTemplate.RspModel.class);
        System.out.println(rspModel);
        long time2 = System.currentTimeMillis();

        System.out.println("time2-time1="+(time2-time1));

    }







}
