package com.easyproject.client.controller.chat;

import com.easyproject.clientbus.domain.dto.FeedbackDto;
import com.easyproject.clientbus.domain.dto.LikeOrStepOnDto;
import com.easyproject.clientbus.domain.dto.TextToSpeechDto;
import com.easyproject.clientbus.service.chat.IChatAiService;
import com.easyproject.common.annotation.RepeatSubmit;
import com.easyproject.common.core.domain.AjaxClientResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 方法说明: 聊天部分
 * @param
 * @return
 * @author java-101
 * @date 2025-03-23 15:08:32
 */
@Slf4j
@RestController
@RequestMapping("/v1/chat")
public class ChatAiController {

    @Resource
    private IChatAiService chatAiService;

    /**
     * 方法说明: 发起消息-通过流式返回-coze平台
     * @param message 用户发送的消息内容
     * @param conversationId 会话ID
     * @return
     * @author java-101
     * @date 2025-03-23 15:12:29
     */
    @GetMapping(value = "/sendMsg", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> send(@RequestParam String message, @RequestParam String conversationId) {
        return chatAiService.chatSendStream(message,conversationId);
    }

    /**
     * 方法说明: 语音转文字
     * @param
     * @return
     * @author java-101
     * @date 2025-03-23 19:01:21
     */
    @RepeatSubmit(interval = 3000, message = "请勿频繁操作")
    @PostMapping("/transcribe")
    public AjaxClientResult transcribe(@RequestParam("file") MultipartFile file) {
        try {
            // 基础校验
            if (file.isEmpty()) {
                return AjaxClientResult.error("请选择要上传的文件");
            }
            if (!Objects.requireNonNull(file.getOriginalFilename()).endsWith(".mp3")) {
                return AjaxClientResult.error("仅支持MP3格式文件");
            }
            return chatAiService.transcribeAudio(file);
        } catch (Exception e) {
            return AjaxClientResult.error("转写失败: " + e.getMessage());
        }
    }

    /**
     * 方法说明: 文字转语音
     * @param
     * @return
     * @author java-101
     * @date 2025-03-23 21:03:42
     */
    @RepeatSubmit(interval = 3000, message = "请勿频繁操作")
    @PostMapping("/textToSpeech")
    public AjaxClientResult textToSpeech(@RequestBody TextToSpeechDto dto) {
        try {
            return chatAiService.textToSpeech(dto.getText());
        } catch (Exception e) {
            return AjaxClientResult.error("语音合成失败: " + e.getMessage());
        }
    }

    /**
     * 方法说明: 对话点赞/踩
     * @param
     * @return
     * @author java-101
     * @date 2025-04-10 15:37:30
     */
    @RepeatSubmit(interval = 3000, message = "请勿频繁操作")
    @PostMapping("/likeOrStepOn")
    public AjaxClientResult likeOrStepOn(@RequestBody LikeOrStepOnDto dto) {
        return chatAiService.likeOrStepOn(dto);
    }

    /**
     * 方法说明: 获取扣子token
     * @param
     * @return
     * @author java-101
     * @date 2025-05-10 15:05:43
     */
    @RepeatSubmit(interval = 3000, message = "请勿频繁操作")
    @GetMapping("/getFeedbackToken")
    public AjaxClientResult getFeedbackToken() {
        return chatAiService.getFeedbackToken();
    }

}
