package com.ai.da.controller;

import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.common.response.Response;
import com.ai.da.mapper.primary.entity.Account;
import com.ai.da.mapper.primary.entity.AccountExtend;
import com.ai.da.mapper.primary.entity.ChatMessage;
import com.ai.da.mapper.primary.entity.TrialOrder;
import com.ai.da.model.dto.*;
import com.ai.da.model.vo.AccountLoginVO;
import com.ai.da.model.vo.AccountPreLoginVO;
import com.ai.da.model.vo.BindEmailVO;
import com.ai.da.model.vo.PersonalHomepageVO;
import com.ai.da.service.AccountService;
import com.ai.da.service.LLMService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


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

    @Resource
    private LLMService llmService;

    @ApiOperation(value = "对话")
    @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);
    }

    @ApiOperation(value = "对话")
    @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);
    }

    @ApiOperation(value = "对话创建项目")
    @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));
    }

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

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