const _ = require('lodash');
const { CMS } = require('../../models/admin/cms');
const { apiResponse } = require("../../core/response/response")
const { SUCCESS, REDIRECTION, CLIENT_ERROR, SERVER_ERROR } = require("../../core/response/statusCode")
const { ERROR_MSG, SUCCESS_MSG } = require("../../core/response/messages")
const mongoose = require("mongoose");
export { }

exports.saveCms = async (req, res) => {
    let lang1 = 'en';
    const { id, privacy_policy_en, privacy_policy_fr, privacy_policy_ar, term_condition_en, term_condition_fr, term_condition_ar, about_us_en, about_us_fr, about_us_ar } = req.body;

    try {
        if (_.has(req.body, ['id']) && (req.body.id) != null) {
            const setting = await CMS.findOneAndUpdate({ _id: new mongoose.Types.ObjectId(req.body.id) }, { $set: req.body }, { new: true });
            return apiResponse(res, false, [], '', SUCCESS.OK, 0, [setting], req);
        } else {
            //setting
            const newcms = new CMS(_.pick(req.body, ['privacy_policy_en', 'privacy_policy_fr', 'privacy_policy_ar', 'term_condition_en', 'term_condition_fr', 'term_condition_ar', 'about_us_en', 'about_us_fr', 'about_us_ar']));
            const record = await newcms.save();
            return apiResponse(res, false, [], '', SUCCESS.OK, 1, [record], req);
        }
    } catch (error) {
        return apiResponse(res, true, [], ERROR_MSG[`SYSTEM-ERROR-${lang1}`], SERVER_ERROR.internalServerError, 0, [], req);
    }

}

exports.fetchCms = async (req, res) => {
    var title_lang = 'en'
    try {



        const [data, count] = await Promise.all([
            CMS.find({}).sort({ createdAt: -1 }),
            CMS.countDocuments()
        ]);
        let result = data.map(record => {
                return {
                    privacy_policy_en: record.privacy_policy_en,
                    privacy_policy_fr: record.privacy_policy_fr,
                    privacy_policy_ar: record.privacy_policy_ar,
                    term_condition_en: record.term_condition_en,
                    term_condition_fr: record.term_condition_fr,
                    term_condition_ar: record.term_condition_ar,
                    about_us_en: record.about_us_en,
                    about_us_fr: record.about_us_fr,
                    about_us_ar: record.about_us_ar,
                    _id: record._id,

                };
            });
        return apiResponse(res, false, [], '', SUCCESS.OK, count, result, req)

    } catch (error) {
        return apiResponse(res, true, [], ERROR_MSG[`SYSTEM-ERROR-${title_lang}`], SERVER_ERROR.internalServerError, 0, [], req)
    }
}


// delete


exports.deleteCms = async (req, res) => {
    let lang = 'en'
    try {
        const { id } = req.body
        let condition = {}
        condition['_id'] = mongoose.Types.ObjectId(id);

        await CMS.findOneAndUpdate(condition, { $set: { is_deleted: true } }, { new: true }).then(responseData => {
            if (responseData) {
                return apiResponse(res, false, [], '', SUCCESS.OK, 0, [],req);
            } else {
                return apiResponse(res, true, [], ERROR_MSG[`SYSTEM-ERROR-${lang}`], SERVER_ERROR.internalServerError, 0, [],req)
            }
        })
    }
    catch (err) {
        return apiResponse(res, true, [], ERROR_MSG['SYSTEM-ERROR'], SERVER_ERROR.internalServerError, 0, [])
    }
}

// cms status active in active
exports.changeCmsStatus = async (req, res) => {
    let lang = 'en'
    const {id, status} = req.body
    let condition = {}
    condition['_id'] =new  mongoose.Types.ObjectId(id);
    let record = await CMS.findOne(condition, { _id: 1 } );
    if (record) {
        await CMS.findOneAndUpdate(condition, { $set: { is_active: status } }, { new: true })
        return apiResponse(res, false,'','', SUCCESS.OK,req)
    }
    return  apiResponse(res, true, [], ERROR_MSG[`NO-RECORD-FOUND-${lang}`], CLIENT_ERROR.badRequest, 0,[],req);
}