package com.pavis.admin.controller.aigc;

import cn.dev33.satoken.annotation.SaIgnore;
import com.pavis.admin.aigc.common.model.req.ChatReq;
import com.pavis.admin.aigc.common.utils.StreamEmitter;
import com.pavis.admin.aigc.service.PvsChatService;
import com.pavis.admin.common.context.UserContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import top.continew.starter.extension.crud.enums.Api;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;

import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import com.pavis.admin.common.controller.BaseController;
import com.pavis.admin.aigc.model.query.AigcAppQuery;
import com.pavis.admin.aigc.model.req.AigcAppReq;
import com.pavis.admin.aigc.model.resp.AigcAppDetailResp;
import com.pavis.admin.aigc.model.resp.AigcAppResp;
import com.pavis.admin.aigc.service.AigcAppService;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * AIGC应用配置管理 API
 *
 * @author semi
 * @since 2025/05/26 17:28
 */
@Tag(name = "AIGC应用配置管理 API")
@RestController
@CrudRequestMapping(value = "/aigc/app", api = {Api.PAGE, Api.GET, Api.CREATE, Api.UPDATE, Api.DELETE, Api.EXPORT})
@AllArgsConstructor

public class AigcAppController extends BaseController<AigcAppService, AigcAppResp, AigcAppDetailResp, AigcAppQuery, AigcAppReq> {
    private final PvsChatService pvsChatService;

    @PostMapping("/chat/completions")
    // @SaCheckPermission("chat:completions")
    @Operation(summary = "聊天接口", description = "聊天接口")
    public SseEmitter chat(@RequestBody ChatReq req) {
        StreamEmitter emitter = new StreamEmitter();
        req.setEmitter(emitter);
        req.setUserId(UserContextHolder.getUserId().toString());
        req.setUsername(UserContextHolder.getUsername());
        ExecutorService executor = Executors.newSingleThreadExecutor();
        req.setExecutor(executor);
        return emitter.streaming(executor, () -> {
            pvsChatService.chat(req);
        });
    }
}