package org.springblade.plugin.workflow.chat.controller;

import io.github.imfangs.dify.client.model.chat.Conversation;
import io.github.imfangs.dify.client.model.chat.ConversationListResponse;
import io.github.imfangs.dify.client.model.chat.MessageListResponse;
import io.github.imfangs.dify.client.model.chat.SuggestedQuestionsResponse;
import lombok.RequiredArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.plugin.workflow.chat.pojo.vo.WfChatMessageVO;
import org.springblade.plugin.workflow.chat.service.IWfChatService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@RestController
@RequestMapping("/chat")
@RequiredArgsConstructor
public class WfChatController {

	private final IWfChatService chatService;

	/**
	 * 发送消息
	 */
	@PostMapping(value = "message", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
	public SseEmitter message(@RequestBody WfChatMessageVO chatMessage) {
		return chatService.chatMessage(chatMessage);
	}

	/**
	 * 上传文件
	 */
	@PostMapping("upload")
	public R<?> upload(@RequestParam("file") MultipartFile file, @RequestParam("appId") Long appId) {
		return chatService.upload(appId, file);
	}

	/**
	 * 停止消息响应
	 */
	@GetMapping("stopMessage")
	public void stopMessage(WfChatMessageVO chatMessage) {
		chatService.stopMessage(chatMessage);
	}

	/**
	 * 消息列表
	 */
	@GetMapping("messages")
	public R<MessageListResponse> messages(WfChatMessageVO chatMessage) {
		return chatService.messages(chatMessage);
	}

	/**
	 * 下一步建议
	 */
	@GetMapping("messageSuggested")
	public R<SuggestedQuestionsResponse> messageSuggested(WfChatMessageVO chatMessage) {
		return chatService.messageSuggested(chatMessage);
	}

	/**
	 * 会话列表
	 */
	@GetMapping("conversations")
	public R<ConversationListResponse> conversations(Long appId, String lastId) {
		return chatService.conversations(appId, lastId);
	}

	/**
	 * 重命名会话
	 */
	@GetMapping("renameConversation")
	public R<Conversation> renameConversation(WfChatMessageVO chatMessage) {
		return chatService.renameConversation(chatMessage);
	}

	/**
	 * 删除会话
	 */
	@DeleteMapping("deleteConversation")
	public void deleteConversation(WfChatMessageVO chatMessage) {
		chatService.deleteConversation(chatMessage);
	}

	/**
	 * 应用基本信息
	 */
	@GetMapping("info")
	public R<?> info(WfChatMessageVO chatMessageVO) {
		return R.data(chatService.info(chatMessageVO));
	}

	/**
	 * 应用参数
	 */
	@GetMapping("parameters")
	public R<?> parameters(WfChatMessageVO chatMessageVO) {
		return R.data(chatService.parameters(chatMessageVO));
	}
}
