let feedbackJSON = require('./nutritionFeedback.json');

exports.getNutritionFeedback = (score) => {

    let name = '';

    if (score.total === 0 || score.total === 0.0) {
        name = 'calScore';
    } else if (score.processed === -1) {
        name = 'processedScore';
    } else {
        let scoreList = {
            calScore: score.total,
            fatScore: score.totalFat,
            satScore: score.satFat,
            tranScore: score.transFat,
            sodiumScore: score.sodium,
            sugarScore: score.sugar,
            processedScore: score.processed,
        };

        //get sorted list of scores
        let min = Object.keys(scoreList).sort(function (a, b) {
            return scoreList[a] - scoreList[b];
        });
        //get first one - the smallest score - to get pick which nutrition message to return
        name = min[0];
    }

    const random = Math.floor(Math.random() * feedbackJSON[name].length);
    return feedbackJSON[name][random];

}
