package com.njcn.gather.plan.controller;

import com.njcn.gather.plan.service.SseClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import static com.njcn.web.utils.RequestUtil.getUserId;

/**
 * SSE控制器，用于处理SSE相关的请求。
 */
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/sse")
@RestController
public class SseController {

    private final SseClient sseClient;


    /**
     * 创建SSE连接。
     *
     * @return SseEmitter对象，用于与客户端建立SSE连接。
     */
    @GetMapping("/createSse")
    public SseEmitter createConnect() {
        String uid = getUserId();
        SseEmitter sse = sseClient.createSse(uid);
        log.info("为用户UID: {} 创建SSE连接", uid);
        return sse;
    }

}