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')
const Complaint = require('../../models/Complaint')


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

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

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

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

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

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

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

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

      Complaint.countDocuments({ read: 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]
    const newComplaintReports = allData[8]


    socket.emit(`admin:allNotifs`, {
      unreadCuMsgs,
      unreadTickets,
      newCustomers,
      newRepresentations,
      newPieceRequests,
      newOldPieceRequests,
      newBufferRequests,
      newGuaranteeReports,
      newComplaintReports
    })
  } 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)
  }
}
