const CONSOLE_METHOD = Object.freeze({ error: 'error', warn: 'warn', info: 'log', debug: 'log' })

export default defineEventHandler(async (event) => {
  const body = await readBody(event).catch(() => ({}))
  const { level, message, data, sessionId, userId } = body
  const prefix = `[CLIENT${sessionId ? `:${sessionId}` : ''}${userId ? `:${userId.slice(0, 8)}` : ''}]`
  const msg = data ? `${message} ${JSON.stringify(data)}` : message
  const method = CONSOLE_METHOD[level] || 'log'
  console[method](prefix, msg)
  return { ok: true }
})
