from django.contrib import admin

from konova.admin import DeletableObjectMixinAdmin
from user.models import User, Team


class UserNotificationAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "name",
        "is_active",
    ]


class UserAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "sso_identifier",
        "username",
        "first_name",
        "last_name",
        "email",
    ]
    fields = [
        "username",
        "first_name",
        "last_name",
        "email",
        "is_active",
        "is_staff",
        "is_superuser",
        "api_token",
        "oauth_token",
        "groups",
        "notifications",
        "date_joined",
        "last_login",
    ]
    search_fields = [
        "username",
        "first_name",
        "last_name",
        "email",
    ]
    filter_horizontal = [
        "groups",
        "notifications",
    ]
    readonly_fields = [
        "date_joined",
        "last_login",
    ]
    autocomplete_fields = [
        "api_token",
    ]
    exclude = [
        "user_permissions",
    ]


class UserActionLogEntryAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "user",
        "timestamp",
        "action",
    ]


class TeamAdmin(DeletableObjectMixinAdmin, admin.ModelAdmin):
    list_display = [
        "name",
        "description",
        "deleted",
    ]
    search_fields = [
        "name",
        "description",
    ]
    filter_horizontal = [
        "users",
        "admins",
    ]

    readonly_fields = [
        "deleted"
    ]

    actions = [
        "restore_deleted_data"
    ]

admin.site.register(User, UserAdmin)
admin.site.register(Team, TeamAdmin)

# Outcommented for a cleaner admin backend on production
#admin.site.register(UserNotification, UserNotificationAdmin)
#admin.site.register(UserActionLogEntry, UserActionLogEntryAdmin)
