from urllib.parse import urlencode

import django_filters

from candidates.models import LoggedAction
from candidates.models.db import EditType
from elections.filters import DSLinkWidget
from moderation_queue.review_required_helper import REVIEW_TYPES


class LoggedActionAPIFilter(django_filters.FilterSet):

    created = django_filters.DateFilter(lookup_expr="gte")

    class Meta:
        model = LoggedAction
        fields = ["action_type", "created"]

    action_type = django_filters.AllValuesMultipleFilter(
        field_name="action_type"
    )


def get_action_types():
    return [
        (x, x)
        for x in LoggedAction.objects.all()
        .values_list("action_type", flat=True)
        .distinct()
    ]


class LoggedActionRecentChangesFilter(django_filters.FilterSet):
    """
    For filtering the recent changes page
    """

    class Meta:
        model = LoggedAction
        fields = ["action_type", "flagged_type", "edit_type", "username"]

    flagged_type = django_filters.ChoiceFilter(
        choices=[
            (t.type, t.label)
            for t in REVIEW_TYPES
            if not t.type.startswith("no_review_")
        ],
        widget=DSLinkWidget(),
    )

    edit_type = django_filters.ChoiceFilter(
        choices=[(edit_type.name, edit_type.value) for edit_type in EditType],
        widget=DSLinkWidget(),
    )

    action_type = django_filters.AllValuesMultipleFilter(
        choices=get_action_types
    )

    username = django_filters.CharFilter(
        label="User name", method="filter_username"
    )

    def filter_username(self, queryset, name, value):
        return queryset.filter(user__username=value)


def recent_changes_filter_shortcuts(request):
    shortcut_list = [
        {
            "name": "humans_only",
            "label": "Humans only",
            "query": {"edit_type": "USER"},
        }
    ]

    query = dict(request.GET)
    shortcuts = {"list": shortcut_list}
    for shortcut in shortcuts["list"]:
        shortcut["querystring"] = urlencode(shortcut["query"], doseq=True)
        if shortcut["query"] == query:
            shortcut["active"] = True
            shortcuts["active"] = shortcut
    return shortcuts

@@ -127,16 +127,28 @@
 b import
+ ActionType,
  EditTyp
@@ -149,16 +149,16 @@
 ditType%0A
-
 from ele
@@ -259,16 +259,72 @@
 TYPES%0A%0A%0A
+def get_action_types():%0A    return ActionType.choices%0A%0A%0A
 class Lo
@@ -554,33 +554,24 @@
 filters.
-AllValues
 Multiple
 Filter(%0A
@@ -566,225 +566,46 @@
 iple
+Choice
 Filter(
-%0A        field_name=%22action_type%22%0A    )%0A%0A%0Adef get_action_types():%0A    return %5B%0A        (x, x)%0A        for x in LoggedAction.objects.all()%0A        .values_list(%22action_type%22, flat=True)%0A        .distinct()%0A    %5D
+choices=get_action_types)
 %0A%0A%0Ac
@@ -1276,25 +1276,16 @@
 ers.
-AllValues
 Multiple
 Filt
@@ -1280,32 +1280,29 @@
 Multiple
+Choice
 Filter(
-%0A        
 choices=
@@ -1317,21 +1317,16 @@
 on_types
-%0A    
 )%0A%0A    u

Use enum choices rather than DB values