from __future__ import annotations

from app.core.api.infrastructure.streaming.sse_response_builder import build_sse_response
from app.core.api.application.stream_service import StreamService


class StreamController:
    def __init__(self, service: StreamService) -> None:
        self._service = service

    async def stream(self, request_id: str):
        queue = await self._service.subscribe(request_id)
        return build_sse_response(
            queue,
            encoder=self._service.encode,
            unsubscribe=lambda: self._service.unsubscribe(request_id, queue),
        )
