const  router = require('express').Router();
let student = require('../models/student.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 to Student');
    }
});
/////////////////////////////////////////////////////////////////////////////////////////////

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

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

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


    if(!user){


        const student_name = req.body.student_name;
        const student_nic = req.body.student_nic;
        const student_email = req.body.student_email;
        const student_phone = req.body.student_phone;
        const student_address = req.body.student_address;
        const student_password = req.body.student_password;
        const student_gender = req.body.student_gender;
        const student_image = req.body.student_image;
        const guardian_name = req.body.guardian_name;
        const guardian_email = req.body.guardian_email;
        const guardian_relationship = req.body.guardian_relationship;

        const newStudent = new student({
            student_name,
            student_nic,
            student_email,
            student_phone,
            student_address,
            student_password,
            student_gender,
            student_image,
            guardian_name,
            guardian_email,
            guardian_relationship
        });
        newStudent.save()
            .then(Student =>{

                const user_id = Student.id.toString();
                const user_nic = student_nic;
                const user_email = student_email;
                const user_password = student_password;
                const user_type = "Student";

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

                });

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

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

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

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

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


                        const content_guardian = `
                        Hi ${guardian_name},\n
                        Your student ${student_name} is Create a Student Account on SmartCoach.\n

                        You can know student progress through the email.
                        We will send Student attendance, Quiz marks, Assignment Marks and other details through this email.

                        Thank You,
                        Team SmartCoach
                        `;

                        var mail_guardian = {
                            from: guardian_name,
                            to: guardian_email,
                            subject: 'Student Information on SmartCoach',
                            text: content_guardian
                        }

                        transporter.sendMail(mail_guardian, (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;
