const  router = require('express').Router();
let classes = require('../models/createClass.model');
let student = require('../models/student.user.model');
let studentList = require('../models/ClassStudentList.model');
let studentRequestList = require('../models/ClassStudentRquestList.model');
const mongoose = require("mongoose");

router.route('/add').post(async(req,res) =>{
    console.log("Inside Create Class")


    if(req.body.institute_id !== ""){
        const tutor_id = mongoose.Types.ObjectId(req.body.tutor_id);
        const class_method = req.body.class_method;
        const class_type = req.body.class_type;
        const institute_id = mongoose.Types.ObjectId(req.body.institute_id);
        const batch_no = req.body.batch_no;
        const class_startingDate = req.body.class_startingDate;
        const subject = req.body.subject;
        const class_fee = req.body.class_fee;
        const class_fee_option = req.body.class_fee_option;
        const isActiveClass = req.body.isActiveClass;
        const isActiveForStudent = req.body.isActiveForStudent;
        const class_conduct_day = req.body.class_conduct_day;
        const class_conduct_time = req.body.class_conduct_time;
        const AdmissionFee = req.body.AdmissionFee;
        const batchYear = req.body.batchYear;

        const newClass = new classes({
            tutor_id,
            class_method,
            class_type,
            batch_no,
            class_startingDate,
            subject,
            class_fee,
            isActiveClass,
            isActiveForStudent,
            institute_id,
            class_fee_option,
            class_conduct_day,
            class_conduct_time,
            AdmissionFee,
            batchYear
        });
        console.log(newClass)

        newClass.save().then(()=>{
            res.json('Class Created!');
        }).catch(err =>res.status(400).json('Error: '+err));
    }
    else {
        const tutor_id = mongoose.Types.ObjectId(req.body.tutor_id);
        const class_method = req.body.class_method;
        const class_type = req.body.class_type;

        const batch_no = req.body.batch_no;
        const class_startingDate = req.body.class_startingDate;
        const subject = req.body.subject;
        const class_fee = req.body.class_fee;
        const class_fee_option = req.body.class_fee_option;
        const isActiveClass = req.body.isActiveClass;
        const isActiveForStudent = req.body.isActiveForStudent;
        const class_conduct_day = req.body.class_conduct_day;
        const class_conduct_time = req.body.class_conduct_time;
        const AdmissionFee = req.body.AdmissionFee;
        const batchYear = req.body.batchYear;

        const newClass = new classes({
            tutor_id,
            class_method,
            class_type,
            batch_no,
            class_startingDate,
            subject,
            class_fee,
            isActiveClass,
            isActiveForStudent,
            class_fee_option,
            class_conduct_day,
            class_conduct_time,
            AdmissionFee,
            batchYear

        });
        console.log(newClass)

        newClass.save().then(()=>{
            res.json('Class Created!');
        }).catch(err =>res.status(400).json('Error: '+err));
    }

});

router.route('/tutorClass/:id').get(function (req, res) {
    let id = req.params.id;
    //console.log("TutorId : " + id)
    classes.find({"tutor_id": id,"isActiveForStudent": true}).populate('institute_id').then(tutorsClasses => {
        ///console.log(tutor);
        res.json(tutorsClasses);
    }).catch(err => res.status(400).json('Eroor: '+ err));
});

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

    const class_id = req.body._id;
    const student_nic = req.body.student_nic;
    const tutor_id = req.body.tutor_id;

    console.log("classID: " + class_id);
    console.log("student_nic: " + student_nic);

    classes.findById(class_id,function(err, tutorsClass){
        if(!tutorsClass)
            req.status(404).send("Sorry Class Could not Found.");
        else {

            student.find({"student_nic": student_nic}).then(student => {

                console.log(student)
                console.log(student[0]._id)

                if(student[0].student_nic === ""){
                    req.status(500).send("Sorry, This Student Not Registered");
                }else{

                    const student_id = student[0]._id;
                    const newClassStudent = new studentList({
                        class_id,
                        student_id,
                        tutor_id
                    });

                    newClassStudent.save().then(result =>{
                        res.json('Student added to the Class!');
                    })
                        .catch(err =>{
                            res.status(500).send("Update not possible");
                        });

                }

            }).catch(err => res.status(500).json('Eroor: '+ err));



        }

    });
});


router.route('/studentList/:id').get(function (req, res) {
    let id = req.params.id;
    console.log("Class ID : " + id)
    studentList.find({"class_id": id}).populate('student_id').then(StudentList => {
        console.log(StudentList);
        res.json(StudentList);
    }).catch(err => {
        console.log(err);
        res.status(400).json('Error: ' + err)
    });
});


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

    const class_id = req.body._id;
    const student_id = req.body.student_id;
    const tutor_id = req.body.tutor_id;

    console.log("classID: " + class_id);
    console.log("student_nic: " + student_id);

    classes.findById(class_id,function(err, tutorsClass){
        if(!tutorsClass)
            res.status(404).send("Sorry Class Could not Found.");
        else {

            student.find({"_id": student_id}).then(student => {

                console.log(student)
                console.log(student[0]._id)

                if(student[0].student_nic === ""){
                    res.status(500).send("Sorry, This Student Not Registered");
                }else{

                    const student_id = student[0]._id;
                    const newClassStudent = new studentRequestList({
                        class_id,
                        student_id,
                        tutor_id
                    });

                    studentRequestList.find({"student_id" : student_id, "class_id" : class_id}).then(reqClass =>{
                        console.log("reqClass : ")
                        console.log(reqClass)
                        if(reqClass.length > 0){
                            res.status(501).send("Already Request to this Class");

                        }
                        else{
                            newClassStudent.save().then(result =>{
                                res.json('Student Requested is complete!');
                            })
                                .catch(err =>{
                                    res.status(500).send("Update not possible");
                                });
                        }
                    })


                }

            }).catch(err => res.status(500).json('Eroor: '+ err));



        }

    });
});

module.exports = router;