package com.yunzhupaas.base.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.entity.AiChatEntity;
import com.yunzhupaas.base.model.ai.AiChatVo;
import com.yunzhupaas.base.model.ai.AiForm;
import com.yunzhupaas.base.model.ai.AiHisVo;
import com.yunzhupaas.base.model.ai.AiParam;
import com.yunzhupaas.base.service.AiChatService;
import com.yunzhupaas.constant.MsgCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "AI助手", description = "Aichat")
@RestController
@RequestMapping("/api/system/Aichat")
public class AiChatController {
    @Autowired
    private AiChatService aiChatService;

    //ai助手接口
    @Operation(summary = "发送对话")
    @Parameters({
            @Parameter(name = "param", description = "对话内容参数"),
    })
    @PostMapping("/send")
    public ActionResult send(@RequestBody AiParam param) {
        String content = aiChatService.send(param.getKeyword());
        return ActionResult.success(MsgCode.SU000.get(), content);
    }

    @Operation(summary = "ai会话列表")
    @GetMapping("/history/list")
    public ActionResult historyList() {
        List<AiChatVo> listVo = aiChatService.historyList();
        return ActionResult.success(listVo);
    }

    @Operation(summary = "ai会话记录")
    @Parameters({
            @Parameter(name = "id", description = "会话id"),
    })
    @GetMapping("/history/get/{id}")
    public ActionResult historyGet(@PathVariable("id") String id) {
        List<AiHisVo> listVo = aiChatService.historyGet(id);
        return ActionResult.success(listVo);
    }

    @Operation(summary = "保存历史记录")
    @Parameters({
            @Parameter(name = "form", description = "会话信息表单"),
    })
    @PostMapping("/history/save")
    public ActionResult historySave(@RequestBody AiForm form) {
        String chatId = aiChatService.historySave(form);
        return ActionResult.success(MsgCode.SU002.get(), chatId);
    }

    @Operation(summary = "删除ai会话")
    @Parameters({
            @Parameter(name = "form", description = "删除ai会话"),
    })
    @DeleteMapping("/history/delete/{id}")
    public ActionResult historyDelete(@PathVariable("id") String id) {
        AiChatEntity byId = aiChatService.getById(id);
        if (byId != null) {
            aiChatService.delete(id);
            return ActionResult.success(MsgCode.SU003.get());
        }
        return ActionResult.fail(MsgCode.FA003.get());
    }
}

