package com.gic.haoban.manage.web.controller.content;

import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.AuthorizedUser;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.content.api.dto.aigc.AIGCContent;
import com.gic.content.api.qdto.aigc.ContentAIGCQDTO;
import com.gic.content.api.qdto.aigc.ContentAiLogDTO;
import com.gic.content.api.service.common.ThirdApiService;
import com.gic.haoban.manage.web.qo.content.ContentAIGCQO;
import com.open.ai.OpenAIUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;

/**
 * 好办-三方接口
 */
@RestController()
public class ContentThirdController {

    private static final Logger log = LoggerFactory.getLogger(ContentThirdController.class);

    @Autowired
    private ThirdApiService thirdApiService;

    /**
     * AIGC生成文案
     * @param aigcQO
     * @return
     */
    @RequestMapping("/third/aigc")
    @ResponseBody
    public RestResponse<AIGCContent> aigc(@RequestBody ContentAIGCQO aigcQO) {
        String clerkId = aigcQO.getClerkId();
        ContentAIGCQDTO aigcQDTO = EntityUtil.changeEntityNew(ContentAIGCQDTO.class, aigcQO);
        aigcQDTO.setUserId(clerkId);
        ServiceResponse<AIGCContent> response = thirdApiService.aigc(aigcQDTO);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        return RestResponse.successResult(response.getResult());
    }

    /**
     * openAI会话
     * @param question
     * @return
     */
    @RequestMapping(value = "/ai/chat", method = RequestMethod.GET, produces = "text/event-stream;charset=UTF-8")
    public SseEmitter openAiChat(String question,String userId,String enterpriseId) {
        SseEmitter emitter = new SseEmitter(30000L);
        OpenAIUtil.chat(emitter, question,userId,enterpriseId);
        return emitter;
    }

    /**
     * OpenAi日志列表
     * @return
     */
    @RequestMapping("/ai/list")
    @ResponseBody
    public RestResponse<List<ContentAiLogDTO>> listOpenAiLog(String userId,String enterpriseId) {
        ServiceResponse<List<ContentAiLogDTO>> response = thirdApiService.listAiLog(enterpriseId, userId);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        return RestResponse.successResult(response.getResult());
    }
}
