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

[((238, 271), 'flask.Blueprint', 'Blueprint', (['"""assignment"""', '__name__'], {}), "('assignment', __name__)\n", (247, 271), False, 'from flask import Blueprint, request, jsonify\n'), ((2024, 2051), 'flaskApp.helpers.getAssignmentData', 'getAssignmentData', (['courseID'], {}), '(courseID)\n', (2041, 2051), False, 'from flaskApp.helpers import getAssignmentData\n'), ((2067, 2082), 'flask.jsonify', 'jsonify', (['result'], {}), '(result)\n', (2074, 2082), False, 'from flask import Blueprint, request, jsonify\n'), ((490, 521), 'flask.jsonify', 'jsonify', (["{'restore': 'success'}"], {}), "({'restore': 'success'})\n", (497, 521), False, 'from flask import Blueprint, request, jsonify\n'), ((858, 870), 'flask.jsonify', 'jsonify', (['res'], {}), '(res)\n', (865, 870), False, 'from flask import Blueprint, request, jsonify\n'), ((1112, 1130), 'flask.request.get_data', 'request.get_data', ([], {}), '()\n', (1128, 1130), False, 'from flask import Blueprint, request, jsonify\n'), ((1227, 1238), 'flask.jsonify', 'jsonify', (['{}'], {}), '({})\n', (1234, 1238), False, 'from flask import Blueprint, request, jsonify\n'), ((1484, 1502), 'flask.request.get_data', 'request.get_data', ([], {}), '()\n', (1500, 1502), False, 'from flask import Blueprint, request, jsonify\n'), ((1602, 1614), 'flask.jsonify', 'jsonify', (['res'], {}), '(res)\n', (1609, 1614), False, 'from flask import Blueprint, request, jsonify\n'), ((571, 586), 'flask.jsonify', 'jsonify', (['e.body'], {}), '(e.body)\n', (578, 586), False, 'from flask import Blueprint, request, jsonify\n'), ((919, 934), 'flask.jsonify', 'jsonify', (['e.body'], {}), '(e.body)\n', (926, 934), False, 'from flask import Blueprint, request, jsonify\n'), ((1299, 1314), 'flask.jsonify', 'jsonify', (['e.body'], {}), '(e.body)\n', (1306, 1314), False, 'from flask import Blueprint, request, jsonify\n'), ((1693, 1708), 'flask.jsonify', 'jsonify', (['e.body'], {}), '(e.body)\n', (1700, 1708), False, 'from flask import Blueprint, request, jsonify\n'), ((2126, 2141), 'flask.jsonify', 'jsonify', (['e.body'], {}), '(e.body)\n', (2133, 2141), False, 'from flask import Blueprint, request, jsonify\n')]