const { body, validationResult } = require('express-validator')
const { _sr } = require('../plugins/serverResponses')
const { res404, res500 } = require('../plugins/controllersHelperFunctions')
const _faSr = _sr.fa
const {  checkValidations } = require('../plugins/controllersHelperFunctions')
const Survey = require('../models/Survey')
const Transaction = require('../models/Transaction')

module.exports.create = [
  (req, res) => {
    const {
      purchasedProduct,
      guaranteeWorkFlow,
      employeeResponsibility,
      serviceQuality,
      serviceTime,
      repairQuality,
      coast,
      description,
      code
    } = req.body

    const data = {
      purchasedProduct,
      guaranteeWorkFlow,
      employeeResponsibility,
      serviceQuality,
      serviceTime,
      repairQuality,
      coast,
      description,
      code,
      type:0,
      user: req.user_id
    }

    const survey = new Survey(data)
    survey.save(err => {
      if (err) return res500(res, err)
      return res.json({ message: _faSr.response.success_save })
    })
  }
]

module.exports.createPublic = [
  [
    body('fullName').notEmpty().withMessage(_faSr.required.name),
    body('national').notEmpty().withMessage(_faSr.required.national_code),
    body('phoneNumber').notEmpty().isMobilePhone('ir-IR').withMessage(_faSr.required.phone_number),
    body('email').notEmpty().isEmail().withMessage(_faSr.required.email),
    body('code').custom(async(value, { req }) => {
      if(value){
        const check =  await Transaction.findOne({ItemReceptionTransNumber:value})
      if(!check){
       return Promise.reject(new Error("کد پذیرش معتبر نیست"))
      }else{
        return Promise.resolve()
      }
      }else{
        return Promise.resolve()
      }
    }),
  ],
  checkValidations(validationResult),
  (req, res) => {
    const {
      purchasedProduct,
      guaranteeWorkFlow,
      employeeResponsibility,
      serviceQuality,
      serviceTime,
      repairQuality,
      coast,
      description,
      code,
      fullName,
      national,
      phoneNumber ,
      email
    } = req.body

    const data = {
      fullName,
      national,
      phoneNumber ,
      email,
      purchasedProduct,
      guaranteeWorkFlow,
      employeeResponsibility,
      serviceQuality,
      serviceTime,
      repairQuality,
      coast,
      description,
      code,
      type:1
    }

    const survey = new Survey(data)

    survey.save(err => {
      if (err) return res500(res, err)
      return res.json({ message: _faSr.response.success_save })
    })

  }
]

module.exports.getAllForUser = [
  (req, res) => {
    Survey.find({ user: req.user_id }, (err, data) => {
      if (err) return res500(res, err)
      return res.json(data)
    })
  }
]

module.exports.getAllForAdmin = [
  (req, res) => {

   // eslint-disable-next-line eqeqeq
   if(req.query.type == 1){
    Survey.find({type: 1 })
    .exec((err, data) => {
      if (err) return res500(res, err)
      return res.json(data)
    })
   }else{
    Survey.find({type: 0 })
    .populate('user', 'first_name last_name')
    .exec((err, data) => {
      if (err) return res500(res, err)
      return res.json(data)
    })
   }
  }
]

module.exports.getOneForAdmin = [
  (req, res) => {
    Survey.findById(req.params.id)
      .populate('user', 'first_name last_name')
      .exec((err, data) => {
        if (err) return res404(res, err)
        return res.json(data)
      })
  }
]
