from django.db.models import Count
from django.urls import reverse_lazy
from django.views import generic
from locations.models import GeoLocation
from scrobbles.stats import get_scrobble_count_qs
from scrobbles.views import ChartContextMixin


class GeoLocationListView(generic.ListView):
    model = GeoLocation
    paginate_by = 100

    def get_queryset(self):
        return (
            super()
            .get_queryset()
            .filter(scrobble__user_id=self.request.user.id)
            .order_by("-scrobble__timestamp")
        )

    def get_context_data(self, **kwargs):
        context_data = super().get_context_data(**kwargs)
        context_data["latest"] = self.get_queryset().first()
        return context_data


class GeoLocationDetailView(ChartContextMixin, generic.DetailView):
    model = GeoLocation
    slug_field = "uuid"


class GeoLocationUpdateView(generic.UpdateView):
    model = GeoLocation
    fields = ["title", "base_run_time_seconds"]
    slug_field = "uuid"
    template_name = "locations/geolocation_form.html"
    success_url = reverse_lazy("locations:geolocation_list")

    def get_success_url(self):
        return reverse_lazy(
            "locations:geolocation_detail", kwargs={"slug": self.object.uuid}
        )
