import {regions} from "../../utils/constans/cities";
import {Users, Orders, GoodsAttributes, Goods} from "../../models/index";
import userNotFound from '../../utils/customErrors/userNotFound';
import forbiddenError from '../../utils/customErrors/forbiddenError';
import {roles} from "../../utils/constans/signUp";
import {adminPrivateKey} from "../../utils/constans/signUp";
const CryptoJS = require("crypto-js");




module.exports.getSignUpLocationController = (req, res, next) => {

    const searchIntersection = [];

    if(req.query.city.length > 0) {

        regions.map(region => {
            region.options.map(city => {
                city = city.value.toLowerCase();
                const requestCity = req.query.city.toLowerCase();

                if( city.indexOf(requestCity) != -1 ) {

                    city = city[0].toUpperCase() + city.slice(1);
                    searchIntersection.push(city + ", " + region.label);
                }
            });
        });
    }

    if(searchIntersection.length == 0 && req.query.city.length > 0) {
        res.send(null);
    }else {
        res.send(searchIntersection);

    }
};


module.exports.signUpController = (req, res, next) => {
    res.send({
        userName: req.body.name,
        role: req.body.role,
        token: req.body.token
    });
};

module.exports.authController = (req, res, next) => {
    res.send({
        userName: req.body.name,
        role: req.body.role,
        token: req.body.token
    });
};

module.exports.checkAuthController = async (req, res, next) => {
    const user = await Users.findOne({ where: {id: req.body.id} });

    if(!user) {
        next(new userNotFound());
    }else {
        res.send({
            userName: user.name,
            role: user.role,
            token: user.token
        });
    }

};

module.exports.getPersonalDataController = async (req, res, next) => {
    if(req.body.role == roles.ADMIN) {
        next(new forbiddenError());
    }else {
        const userData = await Users.findOne({
           where: {
               id: req.body.id
           },
           attributes: {
               exclude: ['createdAt', 'updatedAt', 'token', 'password', 'role', 'id']
           }
        });

        if(!userData) {
            next(new userNotFound());
        }else {
            res.send(userData);
        }

    }
};

module.exports.updatePersonalDataController = (req, res, next) => {
    if(req.body.role == roles.ADMIN) {
        next(new forbiddenError());
    }else {
        Users.findOne({
            where: {
                id: req.body.id
            },
            attributes: ['name']
        }).then(user => {

            user.update(req.body).then(userBeforeUpdate => {
                delete userBeforeUpdate.dataValues.password;
                res.send(userBeforeUpdate);
            });

        }).catch(err => next(new userNotFound()));

    }
};

module.exports.getUserOrdersController = (req, res, next) => {
    res.send(req.body.userOrdersArray);
};

module.exports.deleteOrderController = (req, res, next) => {
    res.send(req.body.userOrdersArray);
};

module.exports.getAllOrdersController = (req, res, next) => {
    res.send(req.body.ordersPackages);
};

module.exports.deleteOrdersByAdminController = (req, res, next) => {
  res.send(req.body.ordersPackages);
};

module.exports.appendOrdersByAdminController = (req, res, next) => {
    res.send(req.body.ordersPackages);
};

module.exports.updateOrdersByAdminController = (req, res, next) => {
    res.send(req.body.ordersPackages);
};