import { Router } from 'express'

import { openAdminRealtimeStream } from '../../services/realtime/realtime-event-service.js'
import { requireAdminRoles } from './session.js'

const router = Router()

router.get('/realtime', requireAdminRoles(['admin', 'operator', 'support']), (req, res) => {
  openAdminRealtimeStream(res, resolveLastEventId(req))
})

function resolveLastEventId(request: {
  get(name: string): string | undefined
  query: Record<string, unknown>
}) {
  return Number(request.get('Last-Event-ID') || request.query.lastEventId || 0) || 0
}

export default router
