const { isAgent } = require('../middlewares/ws_authentication')
const agentEventsController = require('../controllers/agent_EventsController')

module.exports.agentNamespace = () => {
  const io = global._io
  const agent = io.of('/agent')

  // agents middlewares
  agent.use(isAgent)

  // attach events
  agent.on('connection', socket => {
    socket.join(socket.agentId)
    agentEventsController.getAllNotifs(socket)

    /// /////////////////////////////////////////////////////// handle connection error
    socket.on('connect_error', err => {
      console.log(err.message)
    })

    /// ////////////////////////////////////////////////////// events
    socket.on('agent:fetchAnnouncements', data => agentEventsController.fetchAnnouncements(socket))
    socket.on('agent:updateAnnouncement', data => agentEventsController.updateAnnouncement(socket, data))
  })
}
