from django.contrib import admin

from .models import ApiToken, ImpersonationLog, UserProfile


@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('user', 'joined')
    search_fields = ('user__username',)


@admin.register(ApiToken)
class ApiTokenAdmin(admin.ModelAdmin):
    list_display = ('user', 'label', 'key_prefix', 'created_at', 'last_used')
    search_fields = ('user__username', 'label')


@admin.register(ImpersonationLog)
class ImpersonationLogAdmin(admin.ModelAdmin):
    list_display = ('impersonator', 'target', 'reason', 'started_at', 'ended_at')
    list_filter = ('started_at', 'ended_at')
    search_fields = ('impersonator__username', 'target__username', 'reason')
    readonly_fields = ('impersonator', 'target', 'reason', 'started_at', 'ended_at')

    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return request.user.is_superuser