import { messaging } from '../utils/firebase'

export default defineEventHandler(async event => {
  const { topic, registrationToken } = await readBody(event); 
  

  if (!topic) throw createError({ statusCode: 400, statusMessage: 'no-topic'})
  if (!registrationToken) throw createError({ statusCode: 400, statusMessage: 'no-registrationtoken'})
  
  try {
    await messaging.subscribeToTopic([registrationToken], topic)
    
    return { code: 'success'}
  } catch (e) {
    console.log(e)

    throw createError({ statusCode: 500, statusMessage: e.message })
  }
})
