from flask import Flask, redirect, jsonify, session, request
from flask.templating import render_template
from app import app
from MBytes.models import MBytes

# Routes that mostly interact with MorningBytes

#Route to submit morning bytes. TODO improve and return a cleaner feedback
@app.route('/assignment/submit', methods=['POST'])
def submit_answer():
    attempted = MBytes().grade_morning_bytes()
    MBytes().assignments_creator(attempted)
    feedback = ''
    if attempted['result']:
        feedback = f'<h4>Great job,</h4><br>your answer <strong>{attempted["answer"]}</strong> is correct:<br>{attempted["explanation"]}'
    else:
        feedback = f'<h4>Not quite!,</h4><br>the correct answer was <strong>{attempted["answer"]}</strong>:<br>{attempted["explanation"]}'
    feedback = feedback + '<br><br><br>'
    return jsonify({'Success':'Assignment successfully submitted','feedback': feedback, 'resource':attempted['resource']}), 200