
const  router = require('express').Router();
let userAccount = require('../models/userAccount.user.model');
let StudentAccount = require('../models/student.user.model');
let TutorAccount = require('../models/tutor.user.model');
let InstituteAccount = require('../models/institute.user.model');

router.route('/update/:id').post((req,res) =>{
    userAccount.findById(req.params.id, function(err, details){
        if(!details){
            req.status(404).send("data is not found");
        }
        else {
            details.confirmed = true;

            details.save().then( () => {
                res.json('Item update!');
            })
                .catch(err => {
                    res.status(400).send("Update not possible");
                });
        }
    })
});

router.route('/getCredentials').post((req, res) => {

    const email = req.body.user_email;
    const  password = req.body.user_password;

    console.log(email);

    userAccount.find({user_email:email}, function (err, User) {

    }).then(User => {


        console.log(User);
        if(User.length > 0){
            let payload = {
                user_id: User[0].user_id,
                user_email:User[0].user_email,
                user_name:'',
                user_type:User[0].user_type
            }
            console.log(password);
            console.log(User[0].user_password);
            if(User[0].user_password === password){
                console.log("inside password")
                if(User[0].confirmed){
                    console.log("inside Confirmed")
                    if(User[0].user_type === "Student"){
                        console.log("inside student")
                        StudentAccount.findById(User[0].user_id).then(result=>{
                            console.log(result);

                            payload.user_name = result.student_name

                            res.json(payload)
                        })
                    }
                    else if(User[0].user_type === "Tutor"){
                        console.log("inside Tutor")
                        TutorAccount.findById(User[0].user_id).then(result=>{
                            payload.user_name = result.tutor_name
                            res.json(payload)
                        })
                    }
                    else if(User[0].user_type === "Institute"){
                        InstituteAccount.findById(User[0].user_id).then(result=>{
                            payload.user_name = result.institute_name
                            res.json(payload)
                        })
                    }
                }
                else {
                    res.json("Not Verified")
                }


            }
            else {
                res.json("Invalid Password")
            }
        }
        else {
            res.json("Not registered")
        }




    })
        .catch(err => {
            console.log(err);
            res.status(400).json('Error: ' + err)
        });


});


module.exports = router;
