/**
 * Send a test push notification to the current user
 * POST /api/push/test
 *
 * Only available to super-admin users for debugging purposes.
 */

import { sendPushNotificationToUser, isBeamsConfigured } from '../../services/pushNotifications'

export default defineEventHandler(async (event) => {
  const session = await requireUserSession(event)

  // Check if user is super-admin
  if (!session.user.superAdmin) {
    throw createError({
      statusCode: 403,
      statusMessage: 'Forbidden: Super admin access required'
    })
  }

  if (!isBeamsConfigured()) {
    throw createError({
      statusCode: 503,
      statusMessage: 'Push notifications are not configured'
    })
  }

  try {
    const result = await sendPushNotificationToUser(session.user.id, {
      title: 'Test Notification',
      body: 'This is a test push notification!',
      data: {
        type: 'test'
      }
    })

    return {
      success: true,
      message: 'Test notification sent successfully',
      publishId: result?.publishId
    }
  } catch (error: unknown) {
    console.error('[Push] Error sending test notification:', error)

    throw createError({
      statusCode: 500,
      statusMessage: error instanceof Error ? error.message : 'Failed to send test notification'
    })
  }
})
