'use strict';

const PasswordCheckFailedError = require('../errors/password-check-failed-error');

module.exports = function (wagner) {

    return wagner.invoke(function (Console, User) {
        return function (req, res, next) {
            const password = req.body.password;

            if (password !== req.body.passwordChk) {
                return next(new PasswordCheckFailedError('passwords do not match'));
            }

            const user = {
                name: req.body.username,
                description: req.body.description || '',
                auth: {
                    password: password
                },
                account: {
                    email: req.body.email
                }
            };

            Console.log('registering user %s | %s', user.name, user.account.email);

            User.create(user, function (err, created) {
                next(err);
            });
        };
    });

};
