package com.red.circle.other.app.command.ai.qianfan;

import com.alibaba.fastjson.JSON;
import com.red.circle.external.inner.endpoint.aichat.minimax.MiniMaxClient;
import com.red.circle.external.inner.endpoint.aichat.qianfan.QianFanClient;
import com.red.circle.other.app.dto.clientobject.ai.qianfan.QianFanChatCO;
import com.red.circle.other.app.dto.cmd.ai.qianfan.QianFanChatCmd;
import com.red.circle.other.infra.database.rds.entity.ai.qianfan.AiQianFanChat;
import com.red.circle.other.infra.database.rds.service.ai.qianfan.AiQianFanChatService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

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

/**
 * <p>
 * AI 千帆
 * </p>
 *
 * @author pengshigang
 * @since 2022-04-12
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class QianFanChatCmdExe {

    private final QianFanClient qianFanClient;
    private final MiniMaxClient miniMaxClient;
    private final AiQianFanChatService aiQianFanChatService;

    public QianFanChatCO chat(QianFanChatCmd cmd) {
        AiQianFanChat aiQianFanChat = getAiQianFanChat(cmd);
        aiQianFanChatService.save(aiQianFanChat);

//        Map<String, String> stringMap = qianFanClient.chat(cmd.getUserMessage(), cmd.getVoiceId(), cmd.getUserId()).getBody();
        Map<String, String> stringMap = miniMaxClient.chat(cmd.getUserMessage(), cmd.getVoiceId(), cmd.getUserId()).getBody();
        log.info("用户ID={} QianFanChatCmdExe.execute:{}", cmd.getUserId(), stringMap);
        QianFanChatCO qianFanChat = getQianFanChat(stringMap, aiQianFanChat);

        AiQianFanChat updateAiQianFanChat = getAiQianFanChat(aiQianFanChat, qianFanChat);
        aiQianFanChatService.updateSelectiveById(updateAiQianFanChat);

        log.info("用户ID={} QianFanChatCmdExe返回结果:{}", cmd.getUserId(), JSON.toJSONString(qianFanChat));
        return qianFanChat;
    }

    @NotNull
    private static AiQianFanChat getAiQianFanChat(AiQianFanChat aiQianFanChat, QianFanChatCO qianFanChat) {
        AiQianFanChat updateAiQianFanChat = new AiQianFanChat();
        updateAiQianFanChat.setId(aiQianFanChat.getId());
        updateAiQianFanChat.setAiReplyTime(TimestampUtils.now());
        updateAiQianFanChat.setReplyMessage(qianFanChat.getContent());
        updateAiQianFanChat.setOssUrl(qianFanChat.getTtsFile());
        updateAiQianFanChat.setQianFanId(qianFanChat.getQianFanId());
        return updateAiQianFanChat;
    }

    @NotNull
    private static QianFanChatCO getQianFanChat(Map<String, String> stringMap, AiQianFanChat aiQianFanChat) {
        QianFanChatCO qianFanChat = new QianFanChatCO();
        qianFanChat.setUserMessage(aiQianFanChat.getUserMessage());
        qianFanChat.setContent(stringMap.get("content"));
        qianFanChat.setTtsFile(stringMap.get("ttsFile"));
        qianFanChat.setId(aiQianFanChat.getId());
        qianFanChat.setStatus(0);
        qianFanChat.setQianFanId(stringMap.get("id"));
        qianFanChat.setCreateTime(aiQianFanChat.getCreateTime());
        return qianFanChat;
    }

    @NotNull
    private static AiQianFanChat getAiQianFanChat(QianFanChatCmd cmd) {
        AiQianFanChat aiQianFanChat = new AiQianFanChat();
        aiQianFanChat.setUserId(cmd.getUserId());
        aiQianFanChat.setUserMessage(cmd.getUserMessage());
        aiQianFanChat.setVoiceId(cmd.getVoiceId());
        return aiQianFanChat;
    }

    public List<QianFanChatCO> chatList(Long userId) {
        List<AiQianFanChat> list = aiQianFanChatService.query()
                .eq(AiQianFanChat::getUserId, userId)
                .orderByDesc(AiQianFanChat::getId).list();
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().map(
                aiQianFanChat -> {
                    QianFanChatCO qianFanChat = new QianFanChatCO();
                    qianFanChat.setId(aiQianFanChat.getId());
                    qianFanChat.setUserMessage(aiQianFanChat.getUserMessage());
                    qianFanChat.setContent(aiQianFanChat.getReplyMessage());
                    qianFanChat.setTtsFile(aiQianFanChat.getOssUrl());
                    qianFanChat.setStatus(aiQianFanChat.getStatus());
                    qianFanChat.setCreateTime(aiQianFanChat.getCreateTime());
                    qianFanChat.setQianFanId(aiQianFanChat.getQianFanId());
                    return qianFanChat;
                }
        ).toList();
    }

    public void updateQianFanAiChat(Long id, Integer status) {
        aiQianFanChatService.update()
                .eq(AiQianFanChat::getId, id)
                .set(AiQianFanChat::getStatus, status)
                .execute();
    }
}
