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


module.exports.createAward = [
    [
        body('score').notEmpty().isInt().withMessage(_sr.fa.required.field),
        body('title').notEmpty().isString().withMessage(_sr.fa.required.title),
        body('desc').notEmpty().isString().withMessage(_sr.fa.required.description),
        body('expireDate').notEmpty().withMessage(_sr.fa.required.field),
    ],
    checkValidations(validationResult),
    async (req, res) => {
        const { score, title, desc, expireDate } = req.body;
        const fixDate = new Date(expireDate)
        const award = await Award.create({ score, title, desc, expireDate:fixDate })
        res.status(201).json({
            msg: _sr.fa.response.success_save,
            data: award
        })
  

    }
]


module.exports.findAllUser = async (req, res) => {
    const toDay = new Date()
    const awards = await Award.find({expireDate: {$gt: toDay}, isDeleted:false})
    const awardsReq = await AwardRequest.find({userId:req.user_id}).populate('awardId','-created_at -updated_at')
    // eslint-disable-next-line array-callback-return
    const d = awards.map((award) => {

        // eslint-disable-next-line eqeqeq
        const h = awardsReq.filter(awardReq => awardReq.awardId.id == award.id);

        console.log(award);

        if (h.length) {
            return {
                score:award.score,
                title:award.title,
                desc:award.desc,
                expireDate:award.expireDate,
                req: {
                    desc: h[0].desc,
                    metaData: h[0].metaData,
                    status: h[0].status,
                },
                status: 1
            };
        } else {
            return {
                score:award.score,
                title:award.title,
                desc:award.desc,
                expireDate:award.expireDate,
                status: 0
            };
        }
    })

    res.status(200).json(d)
}

module.exports.findAllAdmin = async (req, res) => {
    const data = await Award.find({isDeleted:false})
    res.status(200).json(data)
}

module.exports.findOne = [
    [
        param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید')
    ],
    checkValidations(validationResult),
    async (req, res) => {
        const data = await Award.findById(req.params.id)
        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('score').notEmpty().isInt().withMessage(_sr.fa.required.field),
        body('title').notEmpty().isString().withMessage(_sr.fa.required.title),
        body('desc').notEmpty().isString().withMessage(_sr.fa.required.description),
        body('expireDate').notEmpty().withMessage(_sr.fa.required.field),
    ],
    checkValidations(validationResult),
    async (req, res) => {
        const { score, title, desc, expireDate } = req.body;
        const data = await Award.findById(req.params.id)
        if (!data) {
            res.status(404).json({ msg: _sr.fa.not_found.item_id })
        } else {
            data.score = score;
            data.title = title;
            data.desc = desc;
            data.expireDate = new Date(expireDate)
            data.save()
            res.status(200).json({
                msg: _sr.fa.response.success_save,
                data
            })
    

        }

    }
]


module.exports.delete=[
    [
        param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید'),
    ],
    checkValidations(validationResult),
    async (req, res) => {
        const data = await Award.findById(req.params.id)
        if (!data) {
            res.status(404).json({ msg: _sr.fa.not_found.item_id })
        } else {
            data.isDeleted = true;
            data.expireDate = Date.now()
            data.save()
            res.status(200).json({
                msg: _sr.fa.response.success_remove,
                
            })
    

        }

    }
]