import { getDb } from '../utils/db.js'
import { requireAuth } from '../utils/authHelpers.js'
import { getAlprCameras, parseBbox } from '../utils/alpr.js'

export default defineEventHandler(async (event) => {
  requireAuth(event)
  let bbox
  try {
    bbox = parseBbox(getQuery(event))
  }
  catch (error) {
    throw createError({
      statusCode: error?.statusCode || 400,
      message: error?.message || 'invalid bbox',
    })
  }
  const db = await getDb()
  return getAlprCameras(db, bbox)
})
