import { sendAdminNotification } from '@/src/lib/notif-job'
import { handleOrderCancelled, handleOrderPlaced } from '@/src/lib/post-order-handler'

export const handleNotifQueue =async (batch: any) => {

  for (const message of batch.messages || []) {
    const body = message?.body
    if (!body) {
      console.log('notif_queue message received with empty body')
      continue
    }

    if (body.name === 'send-admin-notification' && body.jobData?.token) {
      await sendAdminNotification({
        token: body.jobData.token,
        title: body.jobData.title,
        body: body.jobData.body,
        imageUrl: body.jobData.imageUrl ?? null,
      })
      continue
    }

    // console.log('notif_queue', body)
  }
}

export const handleOrderPlacedQueue = async (batch: any) => {
  console.log('from the order placed queue handler')
  for (const message of batch.messages || []) {
    const body = message?.body
    if (!body || !Array.isArray(body.orderIds)) {
      console.log('order_placed_queue message received', body)
      continue
    }

    await handleOrderPlaced(body.orderIds)
  }
}

export const handleOrderCancelledQueue = async (batch: any) => {
  for (const message of batch.messages || []) {
    const body = message?.body
    if (!body || !body.orderId) {
      console.log('order_cancelled_queue message received', body)
      continue
    }

    await handleOrderCancelled({
      orderId: body.orderId,
      cancelledBy: body.cancelledBy,
      reason: body.reason,
      cancelledAt: body.cancelledAt,
    })
  }
}
