const { body, param, validationResult } = require('express-validator')
const { _sr } = require('../plugins/serverResponses')
const { checkValidations } = require('../plugins/controllersHelperFunctions')
const User = require('../models/GPS.User')
const Award = require('../models/GPS.Award');
const AwardRequest = require('../models/GPS.AwardRequest');
const { notifyAdmin } = require('../WebSocket/controllers/admin_EventsController')


module.exports.sendRequest = [
    [
        body('awardId').notEmpty().isMongoId().withMessage(_sr.fa.required.field)
    ],
    checkValidations(validationResult),
    async(req, res)=>{
        const award = await Award.findById(req.body.awardId)
        if (!award) {
            res.status(404).json({ msg: _sr.fa.not_found.item_id })
        }else if(award.expireDate < new Date()){
            res.status(404).json({ msg: _sr.fa.not_found.item_id })
        }else{
            const user = await User.findById(req.user_id)
            if(award.score <= user.score){
                user.score -= award.score;
                user.save()
                const data = await AwardRequest.create({
                    awardId:req.body.awardId,
                    userId:req.user_id
                })
                // res.status(200).json({ msg:_sr.fa.response.success_save, data})
                notifyAdmin('newAwardRequest')
                res.status(200).json(data)

            }else{
            res.status(400).json({ msg: "امتیاز شما برای این مورد کافی نیست" })

            }

        }

    }
]

module.exports.findAllUser = async(req, res)=>{
    const data = await AwardRequest.find({userId:req.user_id}).populate('awardId','-created_at -updated_at')
    res.status(200).json(data)
}

module.exports.findAllAdmin = async(req, res)=>{
    const data = await AwardRequest.find().populate('awardId','-created_at -updated_at').populate('userId', 'first_name last_name shopName national_code mobile_number')
    res.status(200).json(data)
}

module.exports.findOne = [
    [
        param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید')
    ],
    checkValidations(validationResult),
    async (req, res) => {
        const data = await AwardRequest.findById(req.params.id).populate('awardId','-created_at -updated_at').populate('userId', 'first_name last_name shopName national_code mobile_number')
        if (!data) {
            res.status(404).json({ msg: _sr.fa.not_found.item_id })
        } else {
            res.status(200).json(data)
        }

    }
]


module.exports.update = [
    [
        param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید'),
        body('status').notEmpty().isInt().withMessage(_sr.fa.required.field),
        body('desc').notEmpty().isString().withMessage(_sr.fa.required.field)
    ],
    checkValidations(validationResult),
    async (req, res) => {
        const { status, desc, metaData } = req.body;
        const data = await AwardRequest.findById(req.params.id)
        if (!data) {
            res.status(404).json({ msg: _sr.fa.not_found.item_id })
        } else {
            await AwardRequest.findByIdAndUpdate(
                req.params.id,
                {
                    status,
                    desc,
                    metaData
                }
            )
            notifyAdmin('newAwardRequest')

            res.status(200).json({ msg: _sr.fa.response.success_save })

        }

    }
]
