const { body, validationResult } = require('express-validator')
const User = require('../models/User')
const SurveyCheck = require('../models/SurveyCheck')
const { res406, res500, checkValidations } = require('../plugins/controllersHelperFunctions')
const sms = require('../plugins/SMS_Module')

module.exports.reception = [
  [
    body('BusinessID').notEmpty().withMessage('BusinessID is required'),
    body('TempReceiptTransNumber').notEmpty().withMessage('TempReceiptTransNumber is required'),
    body('ItemReceptionTransNumber').notEmpty().withMessage('ItemReceptionTransNumber is required')
  ],
  checkValidations(validationResult),
  async (req, res) => {
    const { BusinessID, TempReceiptTransNumber, ItemReceptionTransNumber } = req.body
    try {
      // check user id and agent status
      const user = await User.findOne({ $or: [{ verity_businessID: BusinessID }, { panatech_businessID: BusinessID }] })
      if (!user) return res406(res, 'Incorrect BusinessID')

      await sms.warranty.reception(
        user.mobile_number,
        user.first_name + ' ' + user.last_name,
        TempReceiptTransNumber,
        ItemReceptionTransNumber,
        Date.now()
      )
      return res.json({ message: 'Message sent successfully' })
    } catch (e) {
      return res500(res, e)
    }
  }
]

module.exports.repair = [
  [
    body('BusinessID').notEmpty().withMessage('BusinessID is required'),
    body('TransNumber').notEmpty().withMessage('TransNumber is required'),
    body('ItemReceptionTransNumber').notEmpty().withMessage('ItemReceptionTransNumber is required')
  ],
  checkValidations(validationResult),
  async (req, res) => {
    const { BusinessID, ItemReceptionTransNumber, TransNumber } = req.body
    try {
      // check user id and agent status
      const user = await User.findOne({ $or: [{ verity_businessID: BusinessID }, { panatech_businessID: BusinessID }] })
      if (!user) return res406(res, 'Incorrect BusinessID')

      await sms.warranty.repairCompleted(
        user.mobile_number,
        user.first_name + ' ' + user.last_name,
        ItemReceptionTransNumber,
        TransNumber,
        Date.now()
      )
      return res.json({ message: 'Message sent successfully' })
    } catch (e) {
      return res500(res, e)
    }
  }
]

module.exports.sendBack = [
  [
    body('BusinessID').notEmpty().withMessage('BusinessID is required'),
    body('DeliveryCode').notEmpty().withMessage('DeliveryCode is required'),
    body('ItemReceptionTransNumber').notEmpty().withMessage('ItemReceptionTransNumber is required')
  ],
  checkValidations(validationResult),
  async (req, res) => {
    const { BusinessID, ItemReceptionTransNumber, DeliveryCode } = req.body
    try {
      // check user id and agent status
      const user = await User.findOne({ $or: [{ verity_businessID: BusinessID }, { panatech_businessID: BusinessID }] })
      if (!user) return res406(res, 'Incorrect BusinessID')

      const surveyData = {
        code: ItemReceptionTransNumber,
        user: user._id,
        timeAdded: Date.now()
      }
      const surveyCheck = new SurveyCheck(surveyData)
      surveyCheck.save(err => {
        if (err) console.log('surveyCheck save error', err)
      })

      await sms.warranty.sendback(
        user.mobile_number,
        user.first_name + ' ' + user.last_name,
        ItemReceptionTransNumber,
        DeliveryCode,
        Date.now()
      )
      return res.json({ message: 'Message sent successfully' })
    } catch (e) {
      return res500(res, e)
    }
  }
]
