import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';


export function testController(req, result) {
    console.log("you hit the helper function.");
    req.app.locals.pool.query('SELECT * FROM users', (err, res) => {
        result.send(res.rows);
        pool.end()
    })
}

export function signupController(req, result) {
    bcrypt.hash(req.body.password, 10, function(err, hash){
        if(err) {
            return res.status(500).json({
                error: err
            });
        } else {
            // IF DATABASE IS UPDATED REMEBER TO UPDATE THESE FIELDS AS WELL
            //| ukey | ufname | ulname | udisplay | upicture | uemail | uphone | uposition | urole | upassword | ulastlogin | ualtuser | uavailable
            const query = {
                text: 'INSERT INTO users VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)',
                values: [10, 'Donald', 'Trump', 'Donald Duck', , req.body.email, '3051231234', 'President', 'Admin', hash, , 10, true]
            }
            req.app.locals.pool.query(query, (err, res) => {
                if(err) {
                    console.log(err.stack);
                    result.status(500).json({
                        error:err
                    })
                } else {
                    console.log(res.rows[0]);
                    result.status(200).json({
                        success:'user created'
                    });
                }
            })
        }
    })
}

/*
Step 1  
- Get the users email and search the database to see if such user exist.
- If the response is NULL return with 500 error status 
Step 2
- Use the bcypt.compare method to check wheather a hash password is saved
  inside the database. If TRUE  */
export async function loginController(req, result) {
    const query = {
        text: 'SELECT * FROM users WHERE uemail=$1',
        values: [req.body.email]
    }
    req.app.locals.pool.query(query)
        .then((res) => {
            if(res.rows[0] == null){
                result.status(500).send('error');
            } else {
                bcrypt.compare(req.body.password, res.rows[0].upassword, function(err, resultpass){
                    if(err){
                        return result.status(401).json({
                            failed: 'wrong pass'
                        });
                    }
                    if(resultpass){
                        const JWTToken = jwt.sign({
                            email: res.rows[0].uemail,
                            id:res.rows[0].ukey,
                            position:res.rows[0].uposition
                        },
                        'secret',
                        {
                            expiresIn:'5h'
                        });
                        return result.status(200).json({
                            success:'token created',
                            token: JWTToken,
                            userID: res.rows[0].ukey,
                            userRole: res.rows[0].urole
                        })
                    }
                    return result.status(401).json({
                        failed:'wrong'
                    })
                })
            }
        })
        .catch( (err) => {
            console.log(err.stack);
        })


}
