import django_filters as filters
from django.utils.translation import gettext_lazy as _

from .models import Episode, Page


class PageFilters(filters.FilterSet):
    q = filters.CharFilter(method="search_filter", label=_("Search"))

    class Meta:
        model = Page
        fields = {
            "category__id": ["in"],
            "pub_date": ["exact", "gte", "lte"],
        }

    def search_filter(self, queryset, name, value):
        return queryset.search(value)


class EpisodeFilters(PageFilters):
    podcast = filters.BooleanFilter(
        method="podcast_filter", label=_("Podcast")
    )

    class Meta:
        model = Episode
        fields = PageFilters.Meta.fields.copy()

    def podcast_filter(self, queryset, name, value):
        if value:
            return queryset.filter(sound__is_public=True).distinct()
        return queryset.filter(sound__isnull=True)
