# In your api/admin.py:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser  # Adjust import if needed

class CustomUserAdmin(UserAdmin):
    # Customize the admin interface for CustomUser
    list_display = ('email', 'get_name', 'role', 'is_staff', 'is_active', 'must_change_password')  # Changed 'name' to 'get_name'
    list_filter = ('role', 'is_staff', 'is_active', 'must_change_password')
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Personal info', {'fields': ('first_name', 'last_name')}),
        ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
        ('Custom fields', {'fields': ('role', 'must_change_password')}),
        ('Important dates', {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2', 'role', 'must_change_password'),
        }),
    )
    search_fields = ('email', 'first_name', 'last_name')
    ordering = ('email',)
    filter_horizontal = ('groups', 'user_permissions',)

    # Add this method to display full name in list_display
    def get_name(self, obj):
        full_name = f"{obj.first_name} {obj.last_name}".strip()
        return full_name if full_name else obj.username
    get_name.short_description = 'Name'  # Column header in admin

# Register the CustomUser model with the custom admin class
admin.site.register(CustomUser, CustomUserAdmin)