import sys
from flask import Blueprint, request, jsonify
from flaskApp import db
from flaskApp.assignment.utils import *
from flaskApp.error.error_handlers import *
import json
from flaskApp.helpers import getAssignmentData

assignment = Blueprint('assignment', __name__)

@assignment.route('/restoreAssignment/<calID>/<courseID>', methods=['POST'])
def restore_assignment(calID, courseID):
    try:
        DbAssignmentUtils.restore_all_original_assignment(calID, courseID)
        return jsonify({"restore" : "success"}), 201
    except (NotFound) as e:
        return jsonify(e.body), e.status_code

@assignment.route('/getAssignment/<calID>/<courseID>/<assignment>', methods=['GET'])
def get_assignment_details(calID, courseID, assignment):
    try:
        res = DbAssignmentUtils.get_assignment_slot_details(calID, courseID, assignment)
        return jsonify(res), 200
    except (NotFound) as e:
        return jsonify(e.body), e.status_code

@assignment.route('/deleteAssignment/<calID>/<courseID>', methods=['DELETE'])
def delete_assignment(calID, courseID):
    try:
        request_body = json.loads(request.get_data())
        DbAssignmentUtils.delete_assignment_slot(calID, courseID, request_body)
        return jsonify({}), 204
    except (NotFound, BadRequest) as e:
        return jsonify(e.body), e.status_code

@assignment.route('/addAssignment/<calID>/<courseID>', methods=['POST'])
def add_assignment(calID, courseID):
    try:
        request_body = json.loads(request.get_data())
        res = DbAssignmentUtils.add_Assignment_slot(calID, courseID, request_body)
        return jsonify(res), 201
    except (NotFound, BadRequest, ValidationFailed) as e:
        return jsonify(e.body), e.status_code

'''Test method, keep just in case. Will prob be moved to seperate API designed to
interact with just the MySQL database that the data pipeline will drop stuff into'''
@assignment.route('/getAssignmentTest/<courseID>', methods=['GET'])
def get_session_assignment(courseID):
    try:
        result = getAssignmentData(courseID)
        return jsonify(result)
    except (NotFound) as e:
        return jsonify(e.body), e.status_code

[((9, 13, 9, 46), 'flask.Blueprint', 'Blueprint', ({(9, 23, 9, 35): '"""assignment"""', (9, 37, 9, 45): '__name__'}, {}), "('assignment', __name__)", False, 'from flask import Blueprint, request, jsonify\n'), ((50, 17, 50, 44), 'flaskApp.helpers.getAssignmentData', 'getAssignmentData', ({(50, 35, 50, 43): 'courseID'}, {}), '(courseID)', False, 'from flaskApp.helpers import getAssignmentData\n'), ((51, 15, 51, 30), 'flask.jsonify', 'jsonify', ({(51, 23, 51, 29): 'result'}, {}), '(result)', False, 'from flask import Blueprint, request, jsonify\n'), ((15, 15, 15, 47), 'flask.jsonify', 'jsonify', ({(15, 23, 15, 46): "{'restore': 'success'}"}, {}), "({'restore': 'success'})", False, 'from flask import Blueprint, request, jsonify\n'), ((23, 15, 23, 27), 'flask.jsonify', 'jsonify', ({(23, 23, 23, 26): 'res'}, {}), '(res)', False, 'from flask import Blueprint, request, jsonify\n'), ((30, 34, 30, 52), 'flask.request.get_data', 'request.get_data', ({}, {}), '()', False, 'from flask import Blueprint, request, jsonify\n'), ((32, 15, 32, 26), 'flask.jsonify', 'jsonify', ({(32, 23, 32, 25): '{}'}, {}), '({})', False, 'from flask import Blueprint, request, jsonify\n'), ((39, 34, 39, 52), 'flask.request.get_data', 'request.get_data', ({}, {}), '()', False, 'from flask import Blueprint, request, jsonify\n'), ((41, 15, 41, 27), 'flask.jsonify', 'jsonify', ({(41, 23, 41, 26): 'res'}, {}), '(res)', False, 'from flask import Blueprint, request, jsonify\n'), ((17, 15, 17, 30), 'flask.jsonify', 'jsonify', ({(17, 23, 17, 29): 'e.body'}, {}), '(e.body)', False, 'from flask import Blueprint, request, jsonify\n'), ((25, 15, 25, 30), 'flask.jsonify', 'jsonify', ({(25, 23, 25, 29): 'e.body'}, {}), '(e.body)', False, 'from flask import Blueprint, request, jsonify\n'), ((34, 15, 34, 30), 'flask.jsonify', 'jsonify', ({(34, 23, 34, 29): 'e.body'}, {}), '(e.body)', False, 'from flask import Blueprint, request, jsonify\n'), ((43, 15, 43, 30), 'flask.jsonify', 'jsonify', ({(43, 23, 43, 29): 'e.body'}, {}), '(e.body)', False, 'from flask import Blueprint, request, jsonify\n'), ((53, 15, 53, 30), 'flask.jsonify', 'jsonify', ({(53, 23, 53, 29): 'e.body'}, {}), '(e.body)', False, 'from flask import Blueprint, request, jsonify\n')]