package com.fs.company.controller.company;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.utils.ServletUtils;
import com.fs.company.param.CompanyUserAiWorkflowTtsVoiceParam;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.his.domain.FsAiWorkflow;
import com.fs.his.domain.FsAiWorkflowNode;
import com.fs.his.service.IFsAiWorkflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 企业AI工作流Controller
 *
 * @author fs
 * @date 2026-01-09
 */
@RestController
@RequestMapping("/company/aiWorkflow")
public class CompanyAiWorkflowController extends BaseController {

    @Autowired
    private IFsAiWorkflowService fsAiWorkflowService;

    @Autowired
    private TokenService tokenService;

    /**
     * 分页查询AI工作流列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FsAiWorkflow fsAiWorkflow) {
        startPage();
        List<FsAiWorkflow> list = fsAiWorkflowService.selectFsAiWorkflowList(fsAiWorkflow);
        return getDataTable(list);
    }

    /**
     * 根据企业用户ID获取工作流的关键节点
     * 节点类型: start(开始), end(结束), ai_chat(AI对话)
     */
    @GetMapping("/nodes/{companyUserId}")
    public R getWorkflowNodes(@PathVariable("companyUserId") Long companyUserId) {
        // 定义要查询的节点类型
        List<String> nodeTypes = Arrays.asList("start", "end", "ai_chat");
        List<FsAiWorkflowNode> nodes = fsAiWorkflowService.selectWorkflowNodesByCompanyUserId(companyUserId, nodeTypes);
        return R.ok().put("data", nodes);
    }

    /**
     * 获取当前登录企业用户的工作流关键节点
     * 节点类型: start(开始), end(结束), ai_chat(AI对话)
     */
//    @GetMapping("/myNodes")
    public R getMyWorkflowNode() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser == null || loginUser.getUser() == null) {
            return R.error("用户信息错误");
        }
        Long companyUserId = loginUser.getUser().getUserId();
        List<String> nodeTypes = Arrays.asList("start", "end", "ai_chat");
        List<FsAiWorkflowNode> nodes = fsAiWorkflowService.selectWorkflowNodesByCompanyUserId(companyUserId, nodeTypes);
        return R.ok().put("data", nodes);
    }

    /**
     * 获取当前登录企业用户的工作流及需要录音的节点
     */
    @GetMapping("/myNodes")
    public R getMyWorkflowNodes() {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser == null || loginUser.getUser() == null) {
            return R.error("用户信息错误");
        }
        Long companyUserId = loginUser.getUser().getUserId();
        return R.ok().put("data", fsAiWorkflowService.getMyWorkflowNodes(companyUserId));
    }

//    @PostMapping("/ttsVoice")
//    public R ttsVoice(@RequestBody CompanyUserAiWorkflowTtsVoiceParam param) {
//        return R.ok().put("data", fsAiWorkflowService.ttsVoice(param));
//    }

//    /**
//     * 更新节点的语音URL
//     * @param nodeKey 节点ID
//     * @param voiceUrl 语音URL
//     */
//    @PutMapping("/node/voiceUrl")
//    public R updateNodeVoiceUrl(@RequestParam("nodeKey") String nodeKey,
//                                @RequestParam("voiceUrl") String voiceUrl,
//                                @RequestParam("workflowId") Long workflowId) {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        if (loginUser == null || loginUser.getUser() == null) {
//            return R.error("用户信息错误");
//        }
//        Long companyUserId = loginUser.getUser().getUserId();
//
//        int result = fsAiWorkflowService.updateNodeVoiceUrl(nodeKey, voiceUrl, companyUserId,workflowId);
//        if (result == -1) {
//            return R.error("节点不存在");
//        } else if (result == -2) {
//            return R.error("无权限修改该节点");
//        } else if (result > 0) {
//            return R.ok("更新成功");
//        }
//        return R.error("更新失败");
//    }
}
