const router = require('express').Router();
const StudPer = require('../models/studPer.model');
const axios = require("axios");
// const tutor = require('../models/tutor.user.model');


router.route('/:id').get((req,res) =>{
    StudPer.find({tutor_id : req.params.id}).populate('student_id')
        .then( progressstudents => res.json(progressstudents))
        .catch(err => res.status(400).json('Error :'+err));
});

router.route('/store').post((req ,res) =>{
    const tutor_id = req.body.tutor_id;
    const student_id = req.body.student_id;
    const sex = req.body.sex;
    const guardian = req.body.guardian;
    const study_time = req.body.study_time;
    const activities = req.body.activities;
    const internet = req.body.internet;
    const free_time = req.body.free_time;
    const absences = req.body.absences;
    const g1 = req.body.g1;
    const g2 = req.body.g2;
    const g3 = req.body.g3;

    const newPerformanceDetails = new StudPer({
        tutor_id,
        student_id,
        sex,
        guardian,
        study_time,
        activities,
        internet,
        free_time,
        absences,
        g1,
        g2,
        g3
    });

    newPerformanceDetails.save()
        .then(()=> res.json('Student Performance Details Added!'))
        .catch(err => res.status(400).json('Error :'+err));
});

router.route('/student/:id').get((req , res) =>{
    StudPer.find({student_id : req.params.id}).populate('student_id')
        .then( students => res.json(students))
        .catch(err => res.status(400).json('Error :'+err));
});

//--------------------------Flask API Routes-------------------------------
router.route('/predict_quiz_marks').post(async (req ,res) =>{
    console.log("marks route called");

    let predictDetails = {
        sex : req.body.sex,
        guardian : req.body.guardian,
        studytime : req.body.studytime,
        activities : req.body.activities,
        internet : req.body.internet,
        freetime : req.body.freetime,
        absences : req.body.absences,
        G1 : req.body.G1,
        G2 : req.body.G2,
    }

    console.log(predictDetails);

    let options = {
        uri: 'http://localhost:5000/predict_quiz_marks',
        body: JSON.stringify(predictDetails),
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        }
    }
    let response = await axios.post(options.uri, options.body)
    console.log(response.data);
    res.json(response.data)
});

router.route('/predict_pass_fail').post(async (req ,res) =>{
    console.log("status route called");

    let predictStatusDetails = {
        sex : req.body.sex,
        guardian : req.body.guardian,
        studytime : req.body.studytime,
        activities : req.body.activities,
        internet : req.body.internet,
        freetime : req.body.freetime,
        absences : req.body.absences,
        G1 : req.body.G1,
        G2 : req.body.G2,
    }

    console.log(predictStatusDetails);

    let options = {
        uri: 'http://localhost:5000/predict_pass_fail',
        body: JSON.stringify(predictStatusDetails),
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        }
    }
    let response = await axios.post(options.uri, options.body)
    console.log(response.data);
    res.json(response.data)
});

router.route('/predict_grade').post(async (req ,res) =>{
    console.log("grade route called");

    let predictGradeDetails = {
        sex : req.body.sex,
        guardian : req.body.guardian,
        studytime : req.body.studytime,
        activities : req.body.activities,
        internet : req.body.internet,
        freetime : req.body.freetime,
        absences : req.body.absences,
        G1 : req.body.G1,
        G2 : req.body.G2,
    }

    console.log(predictGradeDetails);

    let options = {
        uri: 'http://localhost:5000/predict_grade',
        body: JSON.stringify(predictGradeDetails),
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        }
    }
    let response = await axios.post(options.uri, options.body)
    console.log(response.data);
    res.json(response.data)
});

module.exports = router;