const Notification = require('../../models/GPS.Notif')

module.exports.getAllNotifs = async socket => {
  try {
    console.log('4');

    const notif = await Notification.find({ userID: socket.userId, read: false }).sort({ updated_at: -1 })
console.log(notif);



    socket.emit(`gps:allNotifs`, notif)
  } catch (e) {
    console.log(e)
  }
}

module.exports.notifyGPS = async (type, userId) => {
  try {
    const io = global._io
    const gps = io.of('/gps')
    const data = { type }

    if (type === 'updateGPSInbox') {
      const gpsInbox = await Notification.find({ userID: userId, read: false }).sort({ updated_at: -1 })
      data.gpsInbox = gpsInbox
    }

    gps.to(userId.toString()).emit(`gps:notify`, data)
  } catch (e) {
    console.log(e)
  }
}



module.exports.fetchNotifications = async (socket) => {
  try {
    console.log('5');

    const notif = await Notification.find({ userID: socket.userId, read: false }).sort({ updated_at: -1 })
    console.log(notif);

    socket.emit('gps:notifications', notif)
  } catch (e) {
    console.log(e)
  }
}
