from django.contrib.auth import get_user_model
from django.utils.translation import gettext_lazy as _

from wagtail.admin.filters import WagtailFilterSet
from wagtail.admin.ui.tables import BooleanColumn, TitleColumn
from wagtail.admin.views.generic.chooser import (
    BaseChooseView,
    ChooseResultsViewMixin,
    ChooseViewMixin,
    CreationFormMixin,
)
from wagtail.admin.views.reports import ReportView

from .forms import AccountChooseFilterForm

User = get_user_model()


# pylint: disable=abstract-method
class BaseAccountChooseView(BaseChooseView):
    filter_form_class = AccountChooseFilterForm

    @property
    def columns(self):
        return [
            TitleColumn(
                name="get_username",
                label=_("Username"),
                url_name=self.chosen_url_name,
                link_attrs={"data-chooser-modal-choice": True},
            ),
            TitleColumn(
                name="get_full_name",
                label=_("Name"),
                url_name=self.chosen_url_name,
                link_attrs={"data-chooser-modal-choice": True},
            ),
            TitleColumn(
                name="email",
                label=_("Email"),
                url_name=self.chosen_url_name,
                link_attrs={"data-chooser-modal-choice": True},
            ),
            BooleanColumn(name="is_active", label=_("Active")),
        ]


class AccountChooseView(ChooseViewMixin, CreationFormMixin, BaseAccountChooseView):
    pass


class AccountChooseResultsView(
    ChooseResultsViewMixin, CreationFormMixin, BaseAccountChooseView
):
    pass


class UserFilterSet(WagtailFilterSet):
    class Meta:
        model = User
        fields = ["is_superuser", "is_active"]


class UserReportView(ReportView):
    template_name = "wagtailadmin/reports/user_report.html"
    filterset_class = UserFilterSet
    header_icon = "user"

    list_export = [
        "id",
        "email",
        "username",
        "first_name",
        "last_name",
        "date_joined",
        "last_login",
        "is_active",
        "is_superuser",
        "is_staff",
    ]

    def get_queryset(self):
        return User.objects.all()
