# Create your views here.
from rest_framework.decorators import api_view
from rest_framework.response import Response

from email_statistics_calculator.calculators import get_email_occurrence_number, count_average_correctness, \
    count_average_duration
from emails.models import Emails


@api_view(('GET',))
def get_email_statistics(request, email_id):
    """
        Function returns email statistics:

        * occurrence number in gameruns
        * average answer duration
        * average answer correctness

        :param request: http request
        :param email_id: id of email
        :returns: statistics for given email
    """
    email = Emails.objects.get(id=email_id)

    occurrence = get_email_occurrence_number(email)
    average_duration = count_average_duration(email)
    average_correctness = count_average_correctness(email)

    return Response({'Times in test': occurrence, 'Average duration': average_duration,
                     'Average correctness': average_correctness})