package com.ruoyi.web.llm.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.noear.solon.ai.chat.message.ChatMessage;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.llm.domain.CmcAgent;
import com.ruoyi.llm.service.ICmcAgentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 智能体Controller
 *
 * @author ruoyi
 * @date 2025-07-17
 */
@RestController
@RequestMapping("/llm/agent")
public class CmcAgentController extends BaseController
{
    @Autowired
    private ICmcAgentService cmcAgentService;

    /**
     * 查询智能体列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcAgent cmcAgent)
    {
        List<CmcAgent> list = cmcAgentService.selectCmcAgentList(cmcAgent);
        return getDataTable(list);
    }

    /**
     * 导出智能体列表
     */
    @Log(title = "智能体", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcAgent cmcAgent)
    {
        List<CmcAgent> list = cmcAgentService.selectCmcAgentList(cmcAgent);
        ExcelUtil<CmcAgent> util = new ExcelUtil<CmcAgent>(CmcAgent.class);
        util.exportExcel(response, list, "智能体数据");
    }

    /**
     * 获取智能体详细信息
     */
    @GetMapping(value = "/{agentId}")
    public AjaxResult getInfo(@PathVariable("agentId") Integer agentId)
    {
        return success(cmcAgentService.selectCmcAgentByAgentId(agentId));
    }

    /**
     * 获取开场白
     * @return
     */
    @GetMapping("/opening")
    public AjaxResult opening(String agentName) {
        return success(ChatMessage.ofAssistant(cmcAgentService.getOpening(agentName)));
    }

    /**
     * 保存目录到Word文件
     */
    @PostMapping("/writeTitles")
    public AjaxResult writeTitles(@RequestBody JSONObject data) throws Throwable
    {
        JSONObject result = cmcAgentService.writeTitles(data);
        if (result.getIntValue("code") == 200) {
            return AjaxResult.success(result.getString("message"));
        } else {
            return AjaxResult.error(result.getString("message"));
        }
    }

    /**
     * 上传单文件
     * @return
     */
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file, String agentName) throws Throwable
    {
        return success(cmcAgentService.uploadDocument(file, agentName));
    }

    /**
     * 上传修改文件
     * @return
     */
    @PostMapping("/modifyFile")
    public AjaxResult uploadModifyFile(String topicId, MultipartFile file, String agentName, String selectedNode) throws Throwable
    {
        return success(cmcAgentService.uploadModifyFile(topicId, file, agentName, selectedNode));
    }

    /**
     * 流式生成章节内容（SSE方式）
     */
    @Anonymous
    @PostMapping(value = "/streamGenerate", produces = "text/event-stream;charset=UTF-8")
    public SseEmitter streamGenerateChapters(String topicId, MultipartFile file, String agentName, String selectedNode) throws Throwable
    {
        return cmcAgentService.streamGenerateChapters(topicId, file, agentName, selectedNode);
    }

    /**
     * 上传多文件
     * @return
     */
    @PostMapping("/uploadList")
    public AjaxResult uploadList(MultipartFile[] fileList, String agentName) throws Throwable
    {
        return success(cmcAgentService.uploadDocumentList(fileList, agentName));
    }

    /**
     * 获取上传进度
     */
    @GetMapping(value = "/getProcess")
    public AjaxResult getProcess() {
        return success(cmcAgentService.getProcess());
    }

    /**
     * 新增智能体
     */
    @Log(title = "智能体", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcAgent cmcAgent)
    {
        cmcAgent.setCreateTime(new Date());
        cmcAgent.setCreateBy(getLoginUser().getUserId().toString());
        return toAjax(cmcAgentService.insertCmcAgent(cmcAgent));
    }

    /**
     * 修改智能体
     */
    @Log(title = "智能体", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcAgent cmcAgent)
    {
        return toAjax(cmcAgentService.updateCmcAgent(cmcAgent));
    }

    /**
     * 删除智能体
     */
    @Log(title = "智能体", businessType = BusinessType.DELETE)
    @DeleteMapping("/{agentIds}")
    public AjaxResult remove(@PathVariable Integer[] agentIds)
    {
        return success(cmcAgentService.deleteCmcAgentByAgentIds(agentIds));
    }
}
