const Reason = require('../models/ContactUsReason')
const {body, validationResult} = require('express-validator')
const dateformat = require('dateformat')
const v_m = require('../validation_messages')


module.exports.create = [
  [
    body('fa_reason')
       .notEmpty().withMessage(v_m['fa'].required.title),

    body('en_reason')
       .notEmpty().withMessage(v_m['fa'].required.title)
  ],
  (req, res) => {
    const errors = validationResult(req)
    if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})

    const data = {
      reason_details: {
        fa: {
          reason: req.body.fa_reason
        },
        en: {
          reason: req.body.en_reason
        }
      },
      created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
    }

    const reason = new Reason(data)
    reason.save(err => {
      if (err) console.log(err)
      Reason.find({}, (err2, data) => {
        if (err2) console.log(err2)
        return res.json(data)
      })
    })
  }
]

module.exports.getAll = [
  (req, res) => {
    Reason.find({}, (err, data) => {
      if (err) console.log(err)
      return res.json(data)
    })
  }
]

module.exports.delete = [
  (req, res) => {
    Reason.findByIdAndDelete(req.params.id, (err, reason) => {
      if (err) return res.status(404).json({message: err})
      return res.json({message: v_m['fa'].response.success_remove})
    })
  }
]
