from datetime import time
from flask import Flask, jsonify, request, session, redirect
from app import db
from pprint import pprint
from uuid import uuid4
import pymongo
from datetime import datetime

#class for MBytes which contains all the models needed in the routes section
class MBytes:
    
    #Get which classes have morning bytes today
    def get_ready_bytes(self):
        roster = list(db.classroster.find({'student_id':session['user']['_id']}))
        classes = []
        for class_ in roster:
            classes.append(db.classes.find_one({'_id':class_['classes_id']}))        
        ready_bytes = []
        for i, rost in enumerate(roster):
            if datetime.today().weekday() in classes[i]['MBytesDays']:
                if (not 'lastModified' in rost) or (rost['lastModified'].date() != datetime.today().date()):
                    ready_bytes.append(classes[i])
                else:
                    # TODO use this to display that the student already completed todays MorningByes
                    pass
            else:
                # TODO This means the class does not have morning bytes today
                pass
        return ready_bytes

    # This will grade the morning bytes
    def grade_morning_bytes(self):
        if 'question_attempted' in session:
            attempted = session['question_attempted']
            del(session['question_attempted'])
            if request.form.get('answer').lower() == attempted['answer'].lower():
                attempted['result'] = 1
            else:
                attempted['result'] = 0 
            
            return attempted

    # Create the assignment for class roster
    def assignments_creator(self, attempted):
        '''
        For simplicity lets make only 1 question at a time
        Information needed
        _id == question the student answered, related to qbdb (Qaka question 3 of the qb)
        right or wrong == 1 or 0
        '''
        class_id = attempted['class_id']
        question_topic = attempted['topic']
        question_number = attempted['question_number']
        result = attempted['result']

        student_id = session['user']['_id']
        roster = db.classroster.find_one({"classes_id":class_id,"student_id":student_id})
        new_assignment = {
            'topic': question_topic,
            'question_number': question_number,
            'result': result
        }

        #Time discrepancies occur when the website and database are hosted on different time zones. This can be solved by using UTC as a standard or taking into consideration local time
        #See https://pymongo.readthedocs.io/en/stable/examples/datetimes.html
        
        if roster and 'assignment' in roster:
            lst = [new_assignment]
            db.classroster.update_one({'_id':roster['_id']},{'$set':{'assignments':lst},'$currentDate':{"lastModified":True}})
        elif roster:
            db.classroster.update_one({'_id':roster['_id']},{'$push':{'assignments': new_assignment},'$currentDate':{"lastModified":True}})
        else:
            # If the program goes here there is a critical error
            pass