from django.contrib import admin
from django.contrib.admin import ModelAdmin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.contrib.auth.models import Group

from apps.accounts.models import Invite, Organization, Role, User

admin.site.unregister(Group)

@admin.register(User)
class UserAdmin(DjangoUserAdmin):
    fieldsets = (
        (None, {'fields': ('email_address', 'password')}),
        ('Personal info', {'fields': ('first_name', 'last_name')}),
        ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'is_manager')}),
        ('Dates', {'fields': ('last_login',)}),
    )

    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email_address', 'first_name', 'last_name', 'password1', 'password2'),
        }),
    )

    list_display = ('email_address', 'first_name', 'last_name', 'is_staff', 'is_manager')
    search_fields = ('email_address', 'first_name', 'last_name')
    ordering = ('email_address',)

@admin.register(Organization)
class OrganizationAdmin(ModelAdmin):
    list_display = ('name', 'owner', 'uuid', 'created_at')
    search_fields = ('name', 'owner__email_address')
    list_filter = ('created_at',)
    raw_id_fields = ('owner',)
    readonly_fields = ('uuid', 'created_at', 'updated_at')

@admin.register(Invite)
class InviteAdmin(ModelAdmin):
    list_display = ('uuid', 'organization', 'created_by', 'is_active', 'uses', 'max_uses', 'expires_at')
    search_fields = ('uuid', 'organization__name', 'created_by__email_address')
    list_filter = ('is_active', 'expires_at')
    raw_id_fields = ('organization', 'created_by')
    readonly_fields = ('uuid', 'created_at')

@admin.register(Role)
class RoleAdmin(ModelAdmin):
    list_display = ('name', 'organization', 'uuid')
    search_fields = ('name', 'organization__name')
    list_filter = ('organization',)
    raw_id_fields = ('organization',)
    readonly_fields = ('uuid', 'created_at', 'updated_at')