import { getDb } from '../utils/db.js'
import { requireAuth } from '../utils/authHelpers.js'
import { rowToDevice, sanitizeDeviceForResponse } from '../utils/deviceUtils.js'

export default defineEventHandler(async (event) => {
  requireAuth(event)
  const { all } = await getDb()
  const rows = await all(
    'SELECT id, name, device_type, vendor, lat, lng, stream_url, source_type, config FROM devices ORDER BY id',
  )
  const devices = rows.map(r => rowToDevice(r)).filter(Boolean)
  return devices.map(sanitizeDeviceForResponse)
})
