package cn.teammodel.controller.frontend;

import cn.teammodel.common.IdRequest;
import cn.teammodel.model.dto.ai.ChatCompletionReqDto;
import cn.teammodel.model.dto.ai.deepseek.DeepSeekChatResponse;
import cn.teammodel.model.dto.ai.deepseek.ChatReqDto;
import cn.teammodel.model.entity.TmdUserDetail;
import cn.teammodel.model.entity.ai.DeepSeekSession;
import cn.teammodel.model.entity.ai.DeepSeekSession.DeepSeekMessage;
import cn.teammodel.security.utils.SecurityUtil;
import cn.teammodel.service.DeepSeekService;
import cn.teammodel.common.R;
import cn.teammodel.service.DeepSeekSessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/aiDeepSeek")
@Api(tags = "AI DeepSeek 能力")
public class AiDeepSeekController {

    private final ExecutorService executorService = Executors.newCachedThreadPool();
    /**
     * 访问DeepSeek方法
     */
    @Resource
    private DeepSeekService deepSeekChatService;

    /**
     * 会话管理
     */
    @Resource
    private DeepSeekSessionService deepSeekSessionService;

    /**
     * 创建默认会话
     * @return
     */
    @PostMapping("session/create/default")
    @ApiOperation("创建默认会话")
    public R<DeepSeekSession> CreateDefaultSession(){
        String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
        String name = (String) ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().get("name");
        DeepSeekSession session = deepSeekSessionService.CreateDefaultSession(userId,name);
        return R.success(session);
    }

    /**
     * 创建会话 按照结构传输
     * @param createSession
     * @return
     */
    @PostMapping("session/create")
    @ApiOperation("创建会话(实体)")
    public R<DeepSeekSession> CreateDefaultSession(@RequestBody @Valid DeepSeekSession createSession){
        DeepSeekSession session = deepSeekSessionService.CreateSession(createSession);
        return R.success(session);
    }

    /**
     * 删除会话
     * @param idRequest
     * @return
     */
    @PostMapping("session/del")
    @ApiOperation("删除会话")
    public R<String> removeSession(@RequestBody @Valid IdRequest idRequest) {
        String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
        deepSeekSessionService.deleteSession(idRequest.getId(), userId);
        return R.success("删除会话成功");
    }

    /**
     * 更新会话
     * @param upSession
     * @return
     */
    @PostMapping("session/update")
    @ApiOperation("更新聊天会话")
    public R<DeepSeekSession> updateSession(@RequestBody @Valid DeepSeekSession upSession) {
        String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
        DeepSeekSession session = deepSeekSessionService.updateSession(upSession, userId);
        return R.success(session);
    }

    /**
     * 查询我的会话
     * @return
     */
    @PostMapping("session/userid")
    @ApiOperation("查询当前登录的会话列表")
    public R<List<DeepSeekSession>> UserId(){
        String userId = SecurityUtil.getLoginUser().getId();
        List<DeepSeekSession> sessions = deepSeekSessionService.UserSessionList(userId);
        return R.success(sessions);
    }

    /**
     * 查询某个会话的聊天记录
     * @param sessionId
     * @return
     */
    @GetMapping("session/history/{sessionId}")
    @ApiOperation("查询当前登录的某个聊天记录")
    public R<List<DeepSeekMessage>> getHistory(@PathVariable String sessionId) {
        String userId = ((TmdUserDetail) SecurityUtil.getAuthentication().getPrincipal()).getClaims().getSubject();
        List<DeepSeekMessage> history = deepSeekSessionService.listHistory(sessionId, userId);
        return R.success(history);
    }

    /**
     *与deepseek的对话
     * @param messageDto
     * @return
     */
    @PostMapping("chat")
    @ApiOperation("单独 与deepseek的对话")
    public R<DeepSeekChatResponse> ChatCompletion(@RequestBody @Valid ChatReqDto messageDto) {
        DeepSeekChatResponse chatResponse = deepSeekChatService.ChatAsk(messageDto);
        return  R.success(chatResponse);
    }

    @PostMapping("okhttp/emitter")
    @ApiOperation("单 与deepseek的对话")
    public SseEmitter ChatEmiter(@RequestBody @Valid ChatReqDto messageDto) {
        return deepSeekChatService.OKHttpChatSeeEmitterAsk(messageDto);
    }

    /**
     * 与deepseek的对话 并保存到数据库中
     * @param chatCompletionReqDto
     * @return
     */
    @PostMapping("chat/completion")
    @ApiOperation("与 spark 的流式对话")
    public SseEmitter chatCompletion(@RequestBody @Valid ChatCompletionReqDto chatCompletionReqDto) {
        String userId = SecurityUtil.getLoginUser().getId();
        SseEmitter sseEmitter = new SseEmitter(-1L);
        return deepSeekChatService.ChatSeeEmitterAsk(chatCompletionReqDto);
    }
    /**
     * 与deepseek的对话 并保存到数据库中
     * @param chatCompletionReqDto
     * @return
     */
    @PostMapping("chat/deep_ponder")
    @ApiOperation("与 spark 的流式对话")
    public SseEmitter deepPonderChatCompletion(@RequestBody @Valid ChatCompletionReqDto chatCompletionReqDto) {
        String userId = SecurityUtil.getLoginUser().getId();
        SseEmitter sseEmitter = new SseEmitter(-1L);
        return deepSeekChatService.ReasonerChatCompletion(chatCompletionReqDto);
    }
}
