from django.core.management import BaseCommand
from django.db.models import Count
from django.db.models.functions import ExtractYear
from books.models import Snapshot, ObjectiveGroupSnapshot
from objectives.models import ObjectiveSnapshot


# Query to group by creator's email, count the snapshots, and order by the count

class Command(BaseCommand):
    def handle(self, *args, **options):
        snapshots_grouped = (Snapshot.objects
                             .annotate(year=ExtractYear('created'))
                             .values('year', 'creator__email')
                             .annotate(count=Count('id'))
                             .order_by('year', '-count'))  # Order by year and then by count (descending)

        # To access the results
        for snapshot in snapshots_grouped:
            modified_email = snapshot['creator__email'].split('@')[0] + '@skillbox.ch'
            print(f"Year: {snapshot['year']}, Creator Email: {modified_email}, Count: {snapshot['count']}")

        hidden_objectives_counter = 0
        custom_objectives_counter = 0

        for snapshot in Snapshot.objects.all():
            if snapshot.hidden_objectives.count() > 0:
                hidden_objectives_counter += 1

            if snapshot.custom_objectives.count() > 0:
                custom_objectives_counter += 1

        print(f"Hidden objectives: {hidden_objectives_counter}")
        print(f"Custom objectives: {custom_objectives_counter}")
        print(f"ObjectiveGroupSnapshot objectives: {ObjectiveGroupSnapshot.objects.count()}")
        print(f"ObjectiveSnapshot objectives: {ObjectiveSnapshot.objects.count()}")
