const Survey = require('../models/Survey')
const { _sr } = require('../plugins/serverResponses')
const { res404, res500 } = require('../plugins/controllersHelperFunctions')
const _faSr = _sr.fa

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,
      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.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) => {
    Survey.find({})
      .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)
      })
  }
]
