var express = require('express')
const router = express.Router();
const db = require("../../utils/database");
const bcrypt = require('bcrypt');
const moment = require('moment');

router.post("/UserCreation", (req, res) => {
    var email = req.body.email
    var username = req.body.username
    var password = req.body.password
    var isAdmin = req.body.isAdmin
    const ofsDate = moment(new Date()).format("DD-MM-YYYY");
    console.log(isAdmin)

    bcrypt.hash(password, 14, function (err, hash) {
        if (err) {
            console.log(err);
            return res.json({
                message: "Error while hashing",
                error: err
            })
        }
        // new Changes 30-sep
        else {
            var query = `insert into finehhdi_fineAD.users (email,username,password,isAdmin,ofsDate) values ("${email}","${username}","${hash}",'${isAdmin}',"${ofsDate}");`
            db.query(query, (err, result) => {
                if (err) {
                    console.log("error ==>", err);
                    if (err.code === "ER_DUP_ENTRY") {
                        res.status(409).json({
                            message: "Duplicate User",
                            results: "Failed",
                        });
                    }
                    else if (err.code === 'ENOTFOUND') {
                        res.status(404).json({
                            message: "Internet Connection Failed",
                            results: 'failed'
                        })

                    }
                    else {
                        console.log(err);
                        res.status(401).json({
                            message: "dep creation failed",
                            results: "Failed",
                        });

                    }
                }
                else {

                    res.status(200).json({
                        message: "dep Created Successfully",
                        results: "Success",
                    });

                }


                // if (err) {
                //     if (err.code === 'ER_DUP_ENTRY') {
                //         res.status(409).json({
                //             message: "Duplicate User",
                //             results: "Failed"
                //         })
                //     }
                // }
                // else {

                //     if (err) {
                //         console.log(err);
                //         res.status(401).json({
                //             message: "User creation failed",
                //             results: "Failed"
                //         })

                //     }

                //     else {

                //         console.log(email, isAdmin)
                //         res.status(200).json({
                //             message: "User Created Successfully",
                //             results: "Success"

                //         })
                //     }

                // }

            })
            //     console.log(hash);
            //     console.log("starting fun");
            //  db.query(`insert into user (email,username,password) values ("${email}","${username}","${hash}")`, (err1, data) => {
            //      if(err1){
            //          return res.json(err1)
            //      }
            //      else{
            //          return res.status(201).json(
            //              {
            //                  message:"User registered successfully",
            //                  result:data
            //              }
            //          )
            //      }
            //       });
        }
    })



})

module.exports = router;