package com.ai.da.controller;

import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.common.response.Response;
import com.ai.da.mapper.primary.entity.ChatMessage;
import com.ai.da.model.dto.*;
import com.ai.da.service.LLMService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
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;

import jakarta.annotation.Resource;
import java.util.*;


@Tag(name = "llm模块")
@Slf4j
@RestController
@RequestMapping("/api/llm")
public class LLMController {

    @Resource
    private LLMService llmService;

    @Operation(summary = "对话")
    @CrossOrigin
    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter streamPrompt(@RequestParam String prompt,
                                   @RequestParam(required = false) Long projectId,
                                   @RequestParam(required = false) String fileUrl,
                                   @RequestParam(required = false) List<String> imageUrlList,
                                   @RequestParam(required = false) String process,
                                   @RequestParam String token,
                                   @RequestParam Boolean enableThinking) {
        return llmService.stream(prompt, projectId, fileUrl, imageUrlList, token, enableThinking, process);
    }

    @Operation(summary = "对话")
    @CrossOrigin
    @GetMapping(value = "/streamNew", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter streamNew(@RequestParam String prompt,
                                   @RequestParam(required = false) Long projectId,
                                   @RequestParam(required = false) String fileUrl,
                                   @RequestParam(required = false) List<String> imageUrlList,
                                   @RequestParam(required = false) String process,
                                   @RequestParam String token,
                                   @RequestParam Boolean enableThinking) {
        return llmService.streamNew(prompt, projectId, fileUrl, imageUrlList, token, enableThinking, process);
    }

    @Operation(summary = "对话创建项目")
    @GetMapping(value = "/chatCreateProject")
    public Response<Long> chatCreateProject(@RequestParam String prompt, @RequestParam String process,
                                            @RequestParam(required = false) String fileUrl,
                                            @RequestParam(required = false) List<String> imageUrlList) {
        return Response.success(llmService.chatCreateProject(prompt, process, fileUrl, imageUrlList));
    }

    @Operation(summary = "上传文件")
    @PostMapping(value = "/uploadFile")
    public Response<List<String>> uploadFile(@RequestParam("file") MultipartFile file) {
        return Response.success(llmService.uploadFile(file));
    }

    @Operation(summary = "获取历史聊天记录")
    @PostMapping(value = "/getChatHistory")
    public Response<PageBaseResponse<ChatMessage>> getChatHistory(@RequestBody ChatHistoryDTO chatHistoryDTO) {
        return Response.success(llmService.getChatHistory(chatHistoryDTO));
    }
}
