package com.ruoyi.web.modules.diagnosis.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.web.modules.diagnosis.domain.AiConsultOpenBody;
import com.ruoyi.web.modules.diagnosis.domain.AiConsultSendResult;
import com.ruoyi.web.modules.diagnosis.domain.AiConsultSessionView;
import com.ruoyi.web.modules.diagnosis.domain.AiConsultTableDataInfo;
import com.ruoyi.web.modules.diagnosis.domain.BizConsultMessage;
import com.ruoyi.web.modules.diagnosis.domain.BizConsultSession;
import com.ruoyi.web.modules.diagnosis.domain.VetConsultSendBody;
import com.ruoyi.web.modules.diagnosis.service.IBizAiOnlineConsultService;
import com.ruoyi.web.modules.diagnosis.support.AiConsultConstants;

/**
 * 移动端 AI 诊断问诊
 */
@RestController
@RequestMapping("/app/consult/ai")
public class BizAppAiConsultController extends BaseController
{
    @Autowired
    private IBizAiOnlineConsultService bizAiOnlineConsultService;

    @Log(title = "移动端新建AI诊断会话", businessType = BusinessType.INSERT)
    @PostMapping("/session")
    public AjaxResult createSession(@RequestBody(required = false) AiConsultOpenBody body)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String name = user.getNickName() != null ? user.getNickName() : user.getUserName();
        AiConsultSessionView session = bizAiOnlineConsultService.createAiSession(user.getUserId(), name, body);
        return success(session);
    }

    @GetMapping("/session/list")
    public AiConsultTableDataInfo list(BizConsultSession query,
            @RequestParam(value = "searchMode", required = false) Boolean searchMode)
    {
        if (query == null)
        {
            query = new BizConsultSession();
        }
        query.setSearchMode(searchMode);
        startPage();
        List<BizConsultSession> list = bizAiOnlineConsultService.selectAiSessionList(query,
                SecurityUtils.getUserId());
        AiConsultTableDataInfo rsp = new AiConsultTableDataInfo();
        rsp.setCode(HttpStatus.SUCCESS);
        rsp.setMsg("查询成功");
        rsp.setRows(list);
        rsp.setTotal(new PageInfo<>(list).getTotal());
        rsp.setDisclaimer(AiConsultConstants.DISCLAIMER);
        return rsp;
    }

    @GetMapping("/session/{sessionId}/messages")
    public AjaxResult messages(@PathVariable("sessionId") Long sessionId,
            @RequestParam(value = "beforeId", required = false) Long beforeId,
            @RequestParam(value = "pageSize", required = false) Integer pageSize)
    {
        List<BizConsultMessage> rows = bizAiOnlineConsultService.selectSessionMessages(sessionId, beforeId, pageSize,
                SecurityUtils.getUserId());
        return success(rows);
    }

    @Log(title = "移动端AI诊断提问", businessType = BusinessType.INSERT)
    @PostMapping("/session/{sessionId}/message")
    public AjaxResult send(@PathVariable("sessionId") Long sessionId, @RequestBody VetConsultSendBody body)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String name = user.getNickName() != null ? user.getNickName() : user.getUserName();
        AiConsultSendResult result = bizAiOnlineConsultService.sendAiMessage(sessionId, body, user.getUserId(), name,
                IpUtils.getIpAddr());
        return success(result);
    }

    @Log(title = "移动端隐藏AI诊断会话", businessType = BusinessType.UPDATE)
    @PostMapping("/session/{sessionId}/hide")
    public AjaxResult hide(@PathVariable("sessionId") Long sessionId)
    {
        return toAjax(bizAiOnlineConsultService.hideSession(sessionId, SecurityUtils.getUserId()));
    }
}
