package com.fs.fastgptApi;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.fastgptApi.param.ChatParam;
import com.fs.fastgptApi.result.ChatDetailTStreamFResult;
import com.fs.fastgptApi.service.ChatService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * swagger 接口
 *

 */
@RestController
@RequestMapping("/test/gtp")
@AllArgsConstructor
public class GtpTestController extends BaseController {

    private final ChatService chatService;


    @ApiOperation("测试AI")
    @GetMapping("/testAI")
    public R testAI(String msg) {
        ChatParam param=new ChatParam();
        param.setChatId("111");
        param.setStream(false);
        param.setDetail(true);

        ChatParam.Variables variables=new ChatParam.Variables();
        variables.setUid("1");
        variables.setName("test");
        param.setVariables(variables);
        List<ChatParam.Message> messageList=new ArrayList<ChatParam.Message>();
        ChatParam.Message message1=new ChatParam.Message();
        message1.setRole("user");
//        message1.setDataId("66f8ef2f6d4b94fbb4f5d40d");
        message1.setContent(msg);
        messageList.add(message1);

        addPromptWord(messageList);
        param.setMessages(messageList);
        R r = chatService.initiatingTakeChat(param,"http://154.8.194.176:3000/api","mygpt-w9Mf0rPxjsoZXjjTnWqfwOtrdT4cJcmuQ2ZdWJnO6HMPuTVan5l8HhKqx");
        if(r.get("code").equals(200)){
            ChatDetailTStreamFResult result=(ChatDetailTStreamFResult)r.get("data");
            if(!result.getChoices().isEmpty()){
            }
        }
        return r;
    }


    private void addPromptWord(List<ChatParam.Message> messageList){
        List<ChatParam.Message> systemMsg = messageList.stream().filter(message -> StringUtils.hasText(message.getContent()) && "user".equals(message.getRole())).map(message -> {
            // 这里获取后台的提示词进行匹配
            String str = "【用户状态信息\n" +
                    "姓名：吴京\n" +
                    "性别：男\n" +
                    "想要的称呼：吴京大王\n" +
                    "年龄：11\n" +
                    "地区：11\n" +
                    "爱好：吃饭\n" +
                    "行为习惯：天天吃饭\n" +
                    "疾病：有毛病\n" +
                    "是否线下就诊：是\n" +
                    "已经患病的时间：11\n" +
                    "目前身体状态：11\n" +
                    "学习到的章节：11\n" +
                    "提及的亲人：大哥\n" +
                    "亲人的疾病：不孕\n" +
                    "交流状态：良好 \n" +
                    "】";
            // 如果没有提示词返回空，后续过滤掉
            if(str.isEmpty()) return null;
            ChatParam.Message msg = new ChatParam.Message();
            msg.setRole("system");
            msg.setContent(str);
            return msg;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        // 提示词不为空的情况下，加入
        if(!systemMsg.isEmpty()){
            messageList.addAll(systemMsg);
        }
    }
}
