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

from .models import Artist, Album
from .mixins import ActiveViewMixin



class HomeView(ActiveViewMixin, TemplateView):
    template_name = "grunge/home.html"
    active_view = "home"


class ArtistListView(ActiveViewMixin, ListView):
    queryset = Artist.objects.all()
    template_name = "grunge/artist_list.html"
    context_object_name = "artists"
    active_view = "artist-list"
    ordering = ("name",)
    paginate_by = 10
    paginate_orphans = 5

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.annotate(
            albums_count=Count("albums", distinct=True),
            tracks_count=Count("albums__tracks", distinct=True),
        )

class AlbumListView(ActiveViewMixin, ListView):
    queryset = Album.objects.all()
    template_name = "grunge/album_list.html"
    context_object_name = "albums"
    active_view = "album-list"
    ordering = ("artist", "name")
    paginate_by = 10
    paginate_orphans = 5

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.annotate(
            tracks_count=Count("tracks", distinct=True),
        )
