from django.db.models import Count, Q
from django.views.generic import TemplateView

from assignments.models import StudentSubmission
from books.models import Module, ContentBlock
from rooms.models import Room
from users.managers import RoleManager
from users.models import SchoolClass, User


class StatisticsView(TemplateView):
    template_name = "statistics.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        private_submissions = StudentSubmission.objects.filter(final=False).count()
        shared_submissions = StudentSubmission.objects.filter(final=True).count()

        private_submissions_by_module = Count('assignments__submissions',
                                              filter=Q(assignments__submissions__final=False))
        shared_submissions_by_module = Count('assignments__submissions', filter=Q(assignments__submissions__final=True))

        modules = Module.objects.values('title').annotate(private_submissions=private_submissions_by_module).annotate(
            shared_submissions=shared_submissions_by_module)

        private_submissions_by_class = Count('users__submissions', filter=Q(users__submissions__final=False))
        shared_submissions_by_class = Count('users__submissions', filter=Q(users__submissions__final=True))
        rooms_by_class = Count('rooms')
        room_entries_by_class = Count('rooms__room_entries')
        room_entries_by_room = Count('room_entries')

        classes = SchoolClass.objects.values('name').annotate(
            private_submissions=private_submissions_by_class).annotate(shared_submissions=shared_submissions_by_class)

        room_classes = SchoolClass.objects.values('name').annotate(room_count=rooms_by_class)
        room_entries_classes = SchoolClass.objects.values('name').annotate(room_entry_count=room_entries_by_class)

        rooms = Room.objects.values('title').annotate(room_entry_count=room_entries_by_room)

        content_block_count = ContentBlock.objects.count()

        content_block_by_users = ContentBlock.objects.filter(
            Q(owner__user_roles__role__key=RoleManager.TEACHER_KEY) | Q(
                owner__user_roles__role__key=RoleManager.STUDENT_KEY)).count()

        content_block_by_teachers = ContentBlock.objects.filter(
            owner__user_roles__role__key=RoleManager.TEACHER_KEY).count()
        content_block_by_students = ContentBlock.objects.filter(
            owner__user_roles__role__key=RoleManager.STUDENT_KEY).count()

        users_with_content_blocks = User.objects.filter(
            Q(user_roles__role__key=RoleManager.STUDENT_KEY) | Q(user_roles__role__key=RoleManager.TEACHER_KEY)).filter(
            owned_pages__gt=0).distinct().count()

        context['private_submissions'] = private_submissions
        context['shared_submissions'] = shared_submissions
        context['modules'] = modules
        context['classes'] = classes
        context['room_classes'] = room_classes
        context['room_entries_classes'] = room_entries_classes
        context['rooms'] = rooms
        context['content_block_count'] = content_block_count
        context['content_block_by_users_count'] = content_block_by_users
        context['content_block_by_teachers_count'] = content_block_by_teachers
        context['content_block_by_students_count'] = content_block_by_students
        context['users_with_content_blocks_count'] = users_with_content_blocks

        return context
