from flask import Flask, redirect, jsonify, session, request
from flask.templating import render_template
from app import app
from QuestionBank.models import QuestionBank
from random import randint
from Classes.models import Class

# Routes that mostly interact with questionbank

# Route to upload questionbank
@app.route('/questionbank/upload', methods=['POST'])
def upload_questionbank():
    obj, code = QuestionBank().new_questionbank()
    if code > 300:
        return (obj, code)
    else:
        return redirect("/dashboard", code=302)

# Route to get questions from a questionbank
@app.route('/qb/get_questions', methods=['GET'])
def tester():
    class_id = request.args.get('class_id')
    topic = Class().get_topic(class_id)
    lst_of_questions = QuestionBank().get_question(topic)
    question_number = randint(0,len(lst_of_questions)-1)
    question = lst_of_questions[question_number]
    class_id = request.args.get('class_id')
    
    session['question_attempted'] = {'class_id': class_id,
                                  'topic': topic,
                                  'question_number': question_number,
                                  'answer': question['answer'],
                                  'resource': question['resource'],
                                  'explanation': question['explanation']}

    del(question['answer'], question['resource'], question['explanation'])
    return jsonify(question), 201
