const router = require(`express`).Router()
const createError = require('http-errors')
const usersModel = require(`../models/users`)

const bcrypt = require('bcryptjs')  // needed for password encryption

const jwt = require('jsonwebtoken')


// IMPORTANT
// Obviously, in a production release, you should never have the code below, as it allows a user to delete a database collection
// The code below is for development testing purposes only 
router.post(`/users/reset_user_collection`, (req, res, next) =>
{
    usersModel.deleteMany({})
    .then(data =>
    {
        const adminPassword = `123-qwe_QWE`
        bcrypt.hash(adminPassword, parseInt(process.env.PASSWORD_HASH_SALT_ROUNDS), (err, hash) =>
        {
            usersModel.create({name: "Administrator", email: "admin@admin.com", password: hash, accessLevel: parseInt(process.env.ACCESS_LEVEL_ADMIN)})
            .then(createData => res.json(createData))
            .catch(err => next(createError(500, `Failed to create Admin user for testing purposes`)))
        })
    })
    .catch(err => next(err))
})


router.post(`/users/register/:name/:email/:password`, (req, res, next) =>
{
    // If a user with this email does not already exist, then create new user
    usersModel.findOne({email: req.params.email})
    .then(uniqueData =>
    {
        if(uniqueData)
        {
            next(createError(403, `User already exists`))
        }
        else
        {
            bcrypt.hash(req.params.password, parseInt(process.env.PASSWORD_HASH_SALT_ROUNDS), (err, hash) =>
            {
                usersModel.create({name: req.params.name, email: req.params.email, password: hash})
                .then(data =>
                {
                    const token = jwt.sign({email: data.email, accessLevel: data.accessLevel}, process.env.JWT_PRIVATE_KEY, {algorithm: 'HS256', expiresIn: process.env.JWT_EXPIRY})

                    res.json({name: data.name, accessLevel: data.accessLevel, token: token})
                })
                .catch(err => next(createError(409, `User was not registered`)))
            })
        }
    })
    .catch(err => next(err))
})


router.post(`/users/login/:email/:password`, (req, res, next) =>
{
    usersModel.findOne({email: req.params.email})
    .then(data =>
    {
        bcrypt.compare(req.params.password, data.password, (err, result) =>
        {
            if(result)
            {
                const token = jwt.sign({email: data.email, accessLevel: data.accessLevel}, process.env.JWT_PRIVATE_KEY, {algorithm: 'HS256', expiresIn: process.env.JWT_EXPIRY})

                res.json({name: data.name, accessLevel: data.accessLevel, token: token})
            }
            else
            {
                next(createError(403, `User is not logged in`))
            }
        })
    })
    .catch(err => next(createError(403, `User is not logged in`)))
})


router.post(`/users/logout`, (req, res, next) =>
{
    res.json({})
})


module.exports = router