package de.oaa.xxx.social;

import de.oaa.xxx.user.UserService;
import org.springframework.http.MediaType;
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 java.security.Principal;

@RestController
@RequestMapping("/events")
public class EventController {

    private final SseService sseService;
    private final UserService userService;

    public EventController(SseService sseService, UserService userService) {
        this.sseService  = sseService;
        this.userService = userService;
    }

    @GetMapping(path = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter stream(Principal principal) {
        return sseService.subscribe(userService.requireUser(principal).getUserId());
    }
}
