from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.contrib.auth.forms import UserChangeForm
from django.utils.translation import gettext as _

from newsreader.accounts.models import User


class UserAdminForm(UserChangeForm):
    class Meta:
        widgets = {
            "email": forms.EmailInput(attrs={"size": "50"}),
        }


class UserAdmin(DjangoUserAdmin):
    list_display = ("email", "last_name", "date_joined", "is_active")
    list_filter = ("is_active", "is_staff", "is_superuser")
    ordering = ("email",)

    search_fields = ["email", "last_name", "first_name"]
    readonly_fields = ("last_login", "date_joined")

    form = UserAdminForm
    fieldsets = (
        (
            _("User settings"),
            {"fields": ("email", "password", "first_name", "last_name", "is_active")},
        ),
        (
            _("Permission settings"),
            {"classes": ("collapse",), "fields": ("is_staff", "is_superuser")},
        ),
        (_("Misc settings"), {"fields": ("date_joined", "last_login")}),
    )


admin.site.register(User, UserAdmin)
