# Generated by smithplates HTTP codegen. Do not edit.

from __future__ import annotations

from typing import Protocol

from fastapi import APIRouter, WebSocket, WebSocketDisconnect
from generated.example.server_event import ServerEvent


class WebsocketHandlers(Protocol):
    async def handle_stream_events(
        self,
        websocket: WebSocket,
        stream_id: str,
    ) -> None: ...


def build_websocket_router(handlers: WebsocketHandlers) -> APIRouter:
    websocket_router = APIRouter()

    async def send_stream_events_message(
        websocket: WebSocket,
        message: ServerEvent,
    ) -> None:
        await websocket.send_text(message.model_dump_json(exclude_none=True))

    @websocket_router.websocket("/streams/{stream_id}/events/ws")
    async def _stream_events_websocket(
        websocket: WebSocket,
        stream_id: str,
    ) -> None:
        await websocket.accept()
        try:
            await handlers.handle_stream_events(
                websocket,
                stream_id=stream_id,
            )
        except WebSocketDisconnect:
            return

    return websocket_router
