package com.ruoyi.aicall.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aicall.domain.CcLlmAgentAccount;
import com.ruoyi.aicall.domain.CcTtsAliyun;
import com.ruoyi.aicall.service.ICcLlmAgentAccountService;
import com.ruoyi.aicall.service.ICcTtsAliyunService;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.aicall.domain.CcInboundLlmAccount;
import com.ruoyi.aicall.service.ICcInboundLlmAccountService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 呼入大模型配置Controller
 * 
 * @author ruoyi
 * @date 2025-06-23
 */
@Controller
@RequestMapping("/aicall/inboundllm")
public class CcInboundLlmAccountController extends BaseController
{
    private String prefix = "aicall/inboundllm";

    @Autowired
    private ICcInboundLlmAccountService ccInboundLlmAccountService;
    @Autowired
    private ICcLlmAgentAccountService llmAgentAccountService;
    @Autowired
    private ICcTtsAliyunService ttsAliyunService;

    @RequiresPermissions("aicall:inboundllm:view")
    @GetMapping()
    public String inboundllm()
    {
        return prefix + "/inboundllm";
    }

    /**
     * 查询呼入大模型配置列表
     */
    @RequiresPermissions("aicall:inboundllm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CcInboundLlmAccount ccInboundLlmAccount)
    {
        startPage();
        List<CcInboundLlmAccount> list = ccInboundLlmAccountService.selectCcInboundLlmAccountList(ccInboundLlmAccount);
        TableDataInfo tableDataInfo = getDataTable(list);
        List<CcInboundLlmAccount> records = (List<CcInboundLlmAccount>) tableDataInfo.getRows();
        for (CcInboundLlmAccount data: records) {
            CcLlmAgentAccount llmAgentAccount = llmAgentAccountService.selectCcLlmAgentAccountById(data.getLlmAccountId());
            if (null != llmAgentAccount) {
                data.setLlmAccountName(llmAgentAccount.getName());
            } else {
                data.setLlmAccountName("");
            }
            if (StringUtils.isNotEmpty(data.getVoiceCode())) {
                CcTtsAliyun ccTtsAliyun = ttsAliyunService.selectCcTtsAliyunByVoiceCode(data.getVoiceCode());
                if (null != ccTtsAliyun) {
                    data.setVoiceSource(ccTtsAliyun.getVoiceSource());
                    data.setVoiceName(ccTtsAliyun.getVoiceName());
                }
            }
            if ("acd".equals(data.getServiceType())) {
                data.setAiTransferGroupId(data.getAiTransferData());
            } else if ("ai".equals(data.getServiceType())) {
                if ("acd".equals(data.getAiTransferType())) {
                    data.setAiTransferGroupId(data.getAiTransferData());
                } else if ("extension".equals(data.getAiTransferType())) {
                    data.setAiTransferExtNumber(data.getAiTransferData());
                } else if ("gateway".equals(data.getAiTransferType())) {
                    if (StringUtils.isNotEmpty(data.getAiTransferData())) {
                        JSONObject aiTransferData = JSONObject.parseObject(data.getAiTransferData());
                        data.setAiTransferGatewayId(aiTransferData.getString("gatewayId"));
                        data.setAiTransferGatewayDestNumber(aiTransferData.getString("destNumber"));
                    }
                }
            }
        }
        tableDataInfo.setRows(records);
        return tableDataInfo;
    }

    /**
     * 导出呼入大模型配置列表
     */
    @RequiresPermissions("aicall:inboundllm:export")
    @Log(title = "呼入大模型配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CcInboundLlmAccount ccInboundLlmAccount)
    {
        List<CcInboundLlmAccount> list = ccInboundLlmAccountService.selectCcInboundLlmAccountList(ccInboundLlmAccount);
        ExcelUtil<CcInboundLlmAccount> util = new ExcelUtil<CcInboundLlmAccount>(CcInboundLlmAccount.class);
        return util.exportExcel(list, "呼入大模型配置数据");
    }

    /**
     * 新增呼入大模型配置
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("ccInboundLlmAccount", new CcInboundLlmAccount());
        return prefix + "/add";
    }

    /**
     * 新增保存呼入大模型配置
     */
    @RequiresPermissions("aicall:inboundllm:add")
    @Log(title = "呼入大模型配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CcInboundLlmAccount ccInboundLlmAccount)
    {
        if ("acd".equals(ccInboundLlmAccount.getServiceType())) {
            ccInboundLlmAccount.setAiTransferData(ccInboundLlmAccount.getAiTransferGroupId());
            ccInboundLlmAccount.setAsrProvider("");
            ccInboundLlmAccount.setVoiceSource("");
            ccInboundLlmAccount.setVoiceCode("");
            ccInboundLlmAccount.setAiTransferType("");
            ccInboundLlmAccount.setIvrId("");
            ccInboundLlmAccount.setLlmAccountId(-1);
        } else if ("ai".equals(ccInboundLlmAccount.getServiceType())) {
            if ("acd".equals(ccInboundLlmAccount.getAiTransferType())) {
                ccInboundLlmAccount.setAiTransferData(ccInboundLlmAccount.getAiTransferGroupId());
            } else if ("extension".equals(ccInboundLlmAccount.getAiTransferType())) {
                ccInboundLlmAccount.setAiTransferData(ccInboundLlmAccount.getAiTransferExtNumber());
            } else if ("gateway".equals(ccInboundLlmAccount.getAiTransferType())) {
                JSONObject aiTransferData = new JSONObject();
                aiTransferData.put("gatewayId", ccInboundLlmAccount.getAiTransferGatewayId());
                aiTransferData.put("destNumber", ccInboundLlmAccount.getAiTransferGatewayDestNumber());
                ccInboundLlmAccount.setAiTransferData(JSONObject.toJSONString(aiTransferData));
            }
            ccInboundLlmAccount.setIvrId("");
        } else if ("ivr".equals(ccInboundLlmAccount.getServiceType())) {
            ccInboundLlmAccount.setAsrProvider("");
            ccInboundLlmAccount.setVoiceSource("");
            ccInboundLlmAccount.setVoiceCode("");
            ccInboundLlmAccount.setAiTransferType("");
            ccInboundLlmAccount.setAiTransferData("");
            ccInboundLlmAccount.setLlmAccountId(-1);
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getVoiceSource())) {
            ccInboundLlmAccount.setVoiceSource("");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getVoiceCode())) {
            ccInboundLlmAccount.setVoiceCode("");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getAsrProvider())) {
            ccInboundLlmAccount.setAsrProvider("");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getAsrLanguageCode())) {
            ccInboundLlmAccount.setAsrLanguageCode("zh-CN");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getTtsLanguageCode())) {
            ccInboundLlmAccount.setTtsLanguageCode("zh-CN");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getAsrModels())) {
            ccInboundLlmAccount.setAsrModels("");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getTtsModels())) {
            ccInboundLlmAccount.setTtsModels("");
        }
        return toAjax(ccInboundLlmAccountService.insertCcInboundLlmAccount(ccInboundLlmAccount));
    }

    /**
     * 修改呼入大模型配置
     */
    @RequiresPermissions("aicall:inboundllm:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CcInboundLlmAccount ccInboundLlmAccount = ccInboundLlmAccountService.selectCcInboundLlmAccountById(id);
//        CcTtsAliyun ccTtsAliyun = ttsAliyunService.selectCcTtsAliyunByVoiceCode(ccInboundLlmAccount.getVoiceCode());
//        ccInboundLlmAccount.setProvider(ccTtsAliyun.getProvider());
//        ccInboundLlmAccount.setVoiceSource(ccTtsAliyun.getVoiceSource());

        if ("acd".equals(ccInboundLlmAccount.getServiceType())) {
            ccInboundLlmAccount.setAiTransferGroupId(ccInboundLlmAccount.getAiTransferData());
        } else if ("ai".equals(ccInboundLlmAccount.getServiceType())) {
            if ("acd".equals(ccInboundLlmAccount.getAiTransferType())) {
                ccInboundLlmAccount.setAiTransferGroupId(ccInboundLlmAccount.getAiTransferData());
            } else if ("extension".equals(ccInboundLlmAccount.getAiTransferType())) {
                ccInboundLlmAccount.setAiTransferExtNumber(ccInboundLlmAccount.getAiTransferData());
            } else if ("gateway".equals(ccInboundLlmAccount.getAiTransferType())) {
                if (StringUtils.isNotEmpty(ccInboundLlmAccount.getAiTransferData())) {
                    JSONObject aiTransferData = JSONObject.parseObject(ccInboundLlmAccount.getAiTransferData());
                    ccInboundLlmAccount.setAiTransferGatewayId(aiTransferData.getString("gatewayId"));
                    ccInboundLlmAccount.setAiTransferGatewayDestNumber(aiTransferData.getString("destNumber"));
                }
            }
        }

        mmap.put("ccInboundLlmAccount", ccInboundLlmAccount);
        return prefix + "/edit";
    }

    /**
     * 修改保存呼入大模型配置
     */
    @RequiresPermissions("aicall:inboundllm:edit")
    @Log(title = "呼入大模型配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CcInboundLlmAccount ccInboundLlmAccount)
    {
        if ("acd".equals(ccInboundLlmAccount.getServiceType())) {
            ccInboundLlmAccount.setAiTransferData(ccInboundLlmAccount.getAiTransferGroupId());
            ccInboundLlmAccount.setAsrProvider("");
            ccInboundLlmAccount.setVoiceSource("");
            ccInboundLlmAccount.setVoiceCode("");
            ccInboundLlmAccount.setAiTransferType("");
            ccInboundLlmAccount.setIvrId("");
            ccInboundLlmAccount.setLlmAccountId(-1);
        } else if ("ai".equals(ccInboundLlmAccount.getServiceType())) {
            if ("acd".equals(ccInboundLlmAccount.getAiTransferType())) {
                ccInboundLlmAccount.setAiTransferData(ccInboundLlmAccount.getAiTransferGroupId());
            } else if ("extension".equals(ccInboundLlmAccount.getAiTransferType())) {
                ccInboundLlmAccount.setAiTransferData(ccInboundLlmAccount.getAiTransferExtNumber());
            } else if ("gateway".equals(ccInboundLlmAccount.getAiTransferType())) {
                JSONObject aiTransferData = new JSONObject();
                aiTransferData.put("gatewayId", ccInboundLlmAccount.getAiTransferGatewayId());
                aiTransferData.put("destNumber", ccInboundLlmAccount.getAiTransferGatewayDestNumber());
                ccInboundLlmAccount.setAiTransferData(JSONObject.toJSONString(aiTransferData));
            }
            ccInboundLlmAccount.setIvrId("");
        } else if ("ivr".equals(ccInboundLlmAccount.getServiceType())) {
            ccInboundLlmAccount.setAsrProvider("");
            ccInboundLlmAccount.setVoiceSource("");
            ccInboundLlmAccount.setVoiceCode("");
            ccInboundLlmAccount.setAiTransferType("");
            ccInboundLlmAccount.setAiTransferData("");
            ccInboundLlmAccount.setLlmAccountId(-1);
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getVoiceSource())) {
            ccInboundLlmAccount.setVoiceSource("");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getVoiceCode())) {
            ccInboundLlmAccount.setVoiceCode("");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getAsrProvider())) {
            ccInboundLlmAccount.setAsrProvider("");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getAsrLanguageCode())) {
            ccInboundLlmAccount.setAsrLanguageCode("zh-CN");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getTtsLanguageCode())) {
            ccInboundLlmAccount.setTtsLanguageCode("zh-CN");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getAsrModels())) {
            ccInboundLlmAccount.setAsrModels("");
        }
        if (StringUtils.isBlank(ccInboundLlmAccount.getTtsModels())) {
            ccInboundLlmAccount.setTtsModels("");
        }
        return toAjax(ccInboundLlmAccountService.updateCcInboundLlmAccount(ccInboundLlmAccount));
    }

    /**
     * 删除呼入大模型配置
     */
    @RequiresPermissions("aicall:inboundllm:remove")
    @Log(title = "呼入大模型配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ccInboundLlmAccountService.deleteCcInboundLlmAccountByIds(ids));
    }

    @ResponseBody
    @GetMapping("/checkCallee")
    public AjaxResult checkCallee(@RequestParam(value = "id", required = false) Integer id, @RequestParam("callee") String callee)
    {
        List<CcInboundLlmAccount> ccInboundLlmAccounts = ccInboundLlmAccountService.selectCcInboundLlmAccountByCallee(callee);
        // caller不存在校验通过
        if (ccInboundLlmAccounts.size() <= 0) {
            return AjaxResult.success(true);
        }
        // 存在但id相同，校验通过
        if (null != id && ccInboundLlmAccounts.get(0).getId().equals(id)) {
            return AjaxResult.success(true);
        }
        return AjaxResult.success(false);
    }


    @ResponseBody
    @GetMapping("/ai/all")
    public AjaxResult getAllAi()
    {
        List<CcInboundLlmAccount> ccInboundLlmAccounts = ccInboundLlmAccountService.selectCcInboundLlmAccountList(new CcInboundLlmAccount().setServiceType("ai"));
        return AjaxResult.success(ccInboundLlmAccounts);
    }
}
