package com.cmvr.web.controller.api;

import com.cmvr.common.core.controller.BaseController;
import com.cmvr.llm.model.CallAgentVO;
import com.cmvr.llm.service.LargeModelRemoteService;
import com.cmvr.llm.util.LargeModelFileUploadUtil;
import com.cmvr.common.core.domain.AjaxResult;
import com.cmvr.system.domain.vo.CarIcon;
import com.cmvr.system.service.CarPathFinderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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


@Api(tags = "大模型服务")
@RestController
@RequestMapping("/api/call")
@RequiredArgsConstructor
public class LargeModelRemoteController  extends BaseController {

    private final LargeModelRemoteService largeModelRemoteService;
    private final CarPathFinderService carPathFinderService;

    @ApiOperation("查找操作路径")
    @GetMapping("/path")
    public AjaxResult path(
            @RequestParam("manufacturer") String manufacturer,
            @RequestParam("vehType") String vehType,
            @RequestParam("startUi") String startUi,
            @RequestParam("targetFeature") String targetFeature
    ) {
        List<CarIcon> path = carPathFinderService.findPath(manufacturer, vehType, startUi, targetFeature);
        return AjaxResult.ok(path.stream().map(CarIcon::getIconName).collect(Collectors.joining(" -> ")));
    }

    @ApiOperation("执行语音转文本工作流")
    @PostMapping("/speechToText")
    public AjaxResult runSpeechToText(@RequestBody CallAgentVO callAgentVO) {
        String result = largeModelRemoteService.runSpeechToTextWorkflow(callAgentVO);
        return AjaxResult.success("工作流执行成功", result);
    }

    @ApiOperation("查询语音转文本结果")
    @PostMapping("/result")
    public AjaxResult queryResult(@RequestParam("processId") String processId) {
        String result = largeModelRemoteService.querySpeechToTextResult(processId);
        return AjaxResult.success("查询成功", result);
    }

    @ApiOperation("上传文件到大模型")
    @PostMapping("/upload/large")
    public String uploadFileLarge(@RequestPart("file") MultipartFile file) throws Exception {
        return LargeModelFileUploadUtil.uploadFile(file.getBytes());
    }
}
