package com.sonic.frog.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.sonic.cow.lib.client.ContentClient;
import com.sonic.cow.lib.enums.PromptTypeEnum;
import com.sonic.cow.lib.input.GenAiUserContentV1Input;
import com.sonic.frog.domain.entity.AiUser;
import com.sonic.frog.domain.entity.AiUserExt;
import com.sonic.frog.service.AiUserExtService;
import com.sonic.frog.service.AiUserService;
import com.sonic.frog.service.MockService;
import com.sonic.frog.utils.DateConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MockServiceImpl implements MockService {

    @Autowired
    private AiUserService aiUserService;
    @Autowired
    private AiUserExtService aiUserExtService;
    @Autowired
    private ContentClient contentClient;

    @Override
    public void updateAiUserExt(Long aiId) throws UnirestException {
        //查询出AI基础信息
        AiUser aiUser = aiUserService.getOne(Wrappers.<AiUser>lambdaQuery().eq(AiUser::getAiId, aiId));
        AiUserExt aiUserExt = aiUserExtService.getAiUserExtByAiId(aiId);

        GenAiUserContentV1Input input = GenAiUserContentV1Input.builder()
                .nickname(aiUser.getNickname())
                .birthday(DateConvertUtils.format(aiUser.getBirthday()))
                .sex(aiUser.getSex().toString())
                .ptType(PromptTypeEnum.GEN_PROFILE_BY_NON)
                .characterCode(aiUser.getCharacterCode())
                .tagCode(aiUser.getTagCode())
                .build();
        //一键生成基础信息
        String userProfile = post(input);

        input.setFigure(userProfile);
        input.setPtType(PromptTypeEnum.GEN_PROLOGUE_BY_NON);
        //一键生成开场白
        String dialoguePrologue = post(input);

        input.setPtType(PromptTypeEnum.GEN_DIALOG_STYLE_BY_NON);
        //一键生成对话风格
        String dialogueStyle = post(input);

        input.setPtType(PromptTypeEnum.GEN_INTRODUCTION);
        //一键生成人物简介
        String introduction = post(input);

        input.setPtType(PromptTypeEnum.EXTRACT_JSON_CONTENT);
        //一键提取json字段
        String userProfileExtJson = post(input);

        //更新数据
        AiUser updateAiUser = AiUser.builder()
                .id(aiUser.getId())
                .introduction(introduction)
                .build();
        aiUserService.updateById(updateAiUser);

        AiUserExt updateAiUserExt = AiUserExt.builder()
                .id(aiUserExt.getId())
                .userProfile(userProfile)
                .userProfileExtJson(userProfileExtJson)
                .dialogueStyle(dialogueStyle)
                .dialoguePrologue(dialoguePrologue)
                .build();
        aiUserExtService.updateById(updateAiUserExt);
    }


    /**
     * 模拟生成
     * @param input
     * @return
     * @throws UnirestException
     */
    public String post(GenAiUserContentV1Input input) throws UnirestException {
        HttpResponse<String> response = Unirest.post("http://test-cow-svc:8080/api/gen/user-content")
//        HttpResponse<String> response = Unirest.post("https://test-cow.crushlevel.ai/mock/gen/user-content")
                .header("Content-Type", "application/json")
                .body(JSONObject.toJSONString(input))
                .asString();
        String body = response.getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);
        return jsonObject.getString("content");
    }



}
