import django_filters
from django.db import models
from django.utils.translation import gettext_lazy as _

from wagtail.admin.filters import WagtailFilterSet

from ocyan.task.models import Task

from ..choices import TaskStatus


class TaskFilterSet(WagtailFilterSet):
    is_highvolume = django_filters.BooleanFilter(
        method="filter_highvolume", required=True
    )

    def __init__(self, data=None, queryset=None, *, request=None, prefix=None):
        if data is None:
            data = {"is_highvolume": False}
        elif "is_highvolume" not in data:
            data = data.copy()
            data["is_highvolume"] = False

        super().__init__(data, queryset, request=request, prefix=prefix)

    # pylint: disable=unused-argument
    def filter_highvolume(self, queryset, name, value):
        if not value:
            return queryset.exclude(
                ~models.Q(status=TaskStatus.FAILED) & models.Q(is_highvolume=True)
            )
        else:
            return queryset.filter(is_highvolume=True)

        return queryset

    class Meta:
        model = Task
        fields = ["status", "is_highvolume"]
