from django.contrib import admin

from authentication.models import ToolshedUser, KnownIdentity, FriendRequestOutgoing, FriendRequestIncoming, \
    AccountPreference, Group, GroupInvite, GroupInviteIncoming


class ToolshedUserAdmin(admin.ModelAdmin):
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'is_active', 'date_joined', 'domain')
    search_fields = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'is_active', 'date_joined', 'domain')


class AccountPreferenceAdmin(admin.ModelAdmin):
    list_display = ('user', 'key', 'value')
    search_fields = ('user__username', 'key')


class KnownIdentityAdmin(admin.ModelAdmin):
    list_display = ('username', 'domain', 'public_key')
    search_fields = ('username', 'domain', 'public_key')


class FriendRequestOutgoingAdmin(admin.ModelAdmin):
    list_display = ('secret', 'befriender_user', 'befriendee_username', 'befriendee_domain')
    search_fields = ('secret', 'befriender_user', 'befriendee_username', 'befriendee_domain')


class FriendRequestIncomingAdmin(admin.ModelAdmin):
    list_display = ('secret', 'befriender_username', 'befriender_domain', 'befriendee_user', 'befriender_public_key')
    search_fields = ('secret', 'befriender_username', 'befriender_domain', 'befriendee_user', 'befriender_public_key')


class GroupAdmin(admin.ModelAdmin):
    list_display = ('name', 'domain', 'get_members')
    search_fields = ('name', 'domain', 'members__username')

    @admin.display(description='Members')
    def get_members(self, obj):
        return ', '.join(str(member) for member in obj.members.all())


class GroupInviteAdmin(admin.ModelAdmin):
    list_display = ('secret', 'group', 'invitee_username', 'invitee_domain')
    search_fields = ('secret', 'group__name', 'invitee_username', 'invitee_domain')


class GroupInviteIncomingAdmin(admin.ModelAdmin):
    list_display = ('secret', 'group_name', 'group_domain', 'inviter_username', 'inviter_domain', 'invitee_user')
    search_fields = ('secret', 'group_name', 'group_domain', 'inviter_username', 'inviter_domain', 'invitee_user')


admin.site.register(ToolshedUser, ToolshedUserAdmin)
admin.site.register(KnownIdentity, KnownIdentityAdmin)
admin.site.register(FriendRequestOutgoing, FriendRequestOutgoingAdmin)
admin.site.register(FriendRequestIncoming, FriendRequestIncomingAdmin)
admin.site.register(AccountPreference, AccountPreferenceAdmin)
admin.site.register(Group, GroupAdmin)
admin.site.register(GroupInvite, GroupInviteAdmin)
admin.site.register(GroupInviteIncoming, GroupInviteIncomingAdmin)
