const User = require('../../models/User')
const ContactPageMessage = require('../../models/ContactPageMessage')
const TicketConversation = require('../../models/TicketConversation')
const Representation = require('../../models/Representation')
const PieceRequest = require('../../models/PieceRequest')
const OldPieceRequest = require('../../models/OldPieceRequest')
const BufferRequest = require('../../models/BufferRequest')
const GuaranteeReport = require('../../models/GuaranteeReport')

module.exports.getAllNotifs = async socket => {
  try {
    const allData = await Promise.all([
      // unreadCuMsgs
      ContactPageMessage.find({ read: false }),

      // unreadTickets
      TicketConversation.find({ messages: { $elemMatch: { isUser: true, read: false } } }),

      // newCustomers
      User.find({ scope: ['user'], seenByAdmin: false, confirmed: true }),

      // newRepresentations
      Representation.find({ status: 'registered' }),

      // newPieceRequests
      PieceRequest.find({ status: 'sent' }),

      // newOldPieceRequests
      OldPieceRequest.find({ status: 'sent' }),

      // newBufferRequests
      BufferRequest.find({ status: 'sent' }),

      // newGuaranteeReports
      GuaranteeReport.find({ status: 'sent', seen: false })
    ])

    const unreadCuMsgs = allData[0]
    const unreadTickets = allData[1]
    const newCustomers = allData[2]
    const newRepresentations = allData[3]
    const newPieceRequests = allData[4]
    const newOldPieceRequests = allData[5]
    const newBufferRequests = allData[6]
    const newGuaranteeReports = allData[7]

    socket.emit(`admin:allNotifs`, {
      unreadCuMsgs: unreadCuMsgs.length,
      unreadTickets: unreadTickets.length,
      newCustomers: newCustomers.length,
      newRepresentations: newRepresentations.length,
      newPieceRequests: newPieceRequests.length,
      newOldPieceRequests: newOldPieceRequests.length,
      newBufferRequests: newBufferRequests.length,
      newGuaranteeReports: newGuaranteeReports.length
    })
  } catch (e) {
    console.log(e)
  }
}

module.exports.notifyAdmin = async type => {
  try {
    const io = global._io
    const admin = io.of('/admin')
    const data = { type }

    if (type === 'updateNewCustomers') {
      const newCustomers = await User.find({ scope: ['user'], seenByAdmin: false })
      data.newCustomers = newCustomers.length
    }

    if (type === 'updateUnreadCuMsgs') {
      const unreadCuMsgs = await ContactPageMessage.find({ read: false })
      data.unreadCuMsgs = unreadCuMsgs.length
    }

    if (type === 'updateUnreadTickets') {
      const unreadTickets = await TicketConversation.find({ messages: { $elemMatch: { isUser: true, read: false } } })
      data.unreadTickets = unreadTickets.length
    }

    if (type === 'updateNewRepresentations') {
      const newRepresentations = await Representation.find({ status: 'registered' })
      data.newRepresentations = newRepresentations.length
    }

    if (type === 'updateNewPieceRequests') {
      const newPieceRequests = await PieceRequest.find({ status: 'sent' })
      data.newPieceRequests = newPieceRequests.length
    }

    if (type === 'updateNewOldPieceRequests') {
      const newOldPieceRequests = await OldPieceRequest.find({ status: 'sent' })
      data.newOldPieceRequests = newOldPieceRequests.length
    }

    if (type === 'updateNewBufferRequests') {
      const newBufferRequests = await BufferRequest.find({ status: 'sent' })
      data.newBufferRequests = newBufferRequests.length
    }

    if (type === 'updateNewGuaranteeReports') {
      const newGuaranteeReports = await GuaranteeReport.find({ status: 'sent', seen: false })
      data.newGuaranteeReports = newGuaranteeReports.length
    }

    admin.emit(`admin:notify`, data)
  } catch (e) {
    console.log(e)
  }
}
