const  router = require('express').Router();
let institute = require('../models/institute.user.model');
let userAccount = require('../models/userAccount.user.model');


const nodemailer = require('nodemailer');
const cred = require('../email-config/config');


////////////////////////////////////////////////////////////////////////////////////////
var transport = {
    host : 'smtp.gmail.com',
    auth : {
        user : cred.USER,
        pass : cred.PASS
    }
}

var transporter = nodemailer.createTransport(transport);

transporter.verify((error, success) => {
    if (error) {
        console.log(error);
    } else {
        console.log('Server is ready to take messages of Institute');
    }
});
/////////////////////////////////////////////////////////////////////////////////////////////



router.route('/').get((req,res) =>{
    institute.find()
        .then(Institutes => res.json(Institutes))
        .catch(err => res.status(400).json('Eroor: '+ err));
});

router.route('/add').post(async(req,res) =>{
    const institute_name = req.body.institute_name;
    const institute_email = req.body.institute_email;
    const institute_phones = req.body.institute_phones;
    const institute_address = req.body.institute_address;
    const institute_city = req.body.institute_city;
    const institute_password = req.body.institute_password;
    const institute_registrationNo = req.body.institute_registrationNo;
    const institute_image = req.body.institute_image;


    //checking if the user is already exist in the database
    const user = await institute.findOne({
        institute_email:req.body.institute_email
    })

    if(!user){
        const newInstitute = new institute({
            institute_name,
            institute_email,
            institute_phones,
            institute_address,
            institute_city,
            institute_password,
            institute_registrationNo,
            institute_image

        });
        newInstitute.save()
            .then(Institute =>{

                const user_id = Institute.id.toString();
                const user_nic = "";
                const user_email = req.body.institute_email;
                const user_password = req.body.institute_password;
                const user_type = "Institute";

                const newUser = new userAccount({
                    user_id,
                    user_nic,
                    user_email,
                    user_password,
                    user_type

                });

                newUser.save()
                    .then(newUser =>{

////////////////////////////////////////////////////////////////////////////////////
                        const content = `
                        Hi ${institute_name},\n
                        You are Create a Institute Account on SmartCoach.\n
                        You can verify your email address click on 'http://localhost:3000/AccountVerified/${newUser.id}'

                        Used institute email address as the username : ${user_email}
                        Used given password as the password
                        Thank You,
                        Team SmartCoach
                    `;

                        var mail = {
                            from: institute_name,
                            to: user_email,
                            subject: 'Email Verification - Institute',
                            text: content
                        }

                        transporter.sendMail(mail, (err, data) => {
                            if (err) {
                                res.json({
                                    msg: 'fail'
                                })
                            } else {
                                res.json({
                                    msg: 'success'
                                })
                            }
                        })
                        ;
/////////////////////////////////////////////////////////////////////////////////////

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

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

    }
    else{
        return res.status(500).json('Email already exit');
    }

});



module.exports = router;
