var express=require('express')
const router = express.Router();
const db = require("../../utils/database");
const dbFuntion=require("../../utils/dbfunction")
const bcrypt=require('bcrypt');
const { response } = require('express');
router.post("/createUser", (req, res) => {
    var username=req.body.username;
    var email=req.body.email;

    var password=req.body.password;
    console.log(username,email,password);
    db.query('Select * from finehhdi_fineAD.users')
    
   
   bcrypt.hash(password,14,function(err,hash,data){
       if(err){
           console.log(err);
           return res.json({
               message:"Error while hashing",
               error:err
           })
       }
       else{
           console.log(hash);
           console.log("starting fun");

        db.query(`insert into finehhdi_fineAD.users (email,username,password) values ("${email.toLowerCase()}","${username}","${hash}")`, (err1, data) => {
            if(err1){
                if(err1){
                    return res.status(409).json({
                        message:"User Already Exist"
                    })
                }
                else{
                    console.log(err1)
                }
                
                
            }
            else{
                return res.status(201).json(
                    {
                        message:"User registered successfully",
                        result:data
                       
                    }
                )
            }
             });
       }
   }) 

    //res.status(200).send({"message":"all working fine"})
}
)

module.exports = router;
