from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import SystemUser, UserProfile, UserAddress
from django.utils.translation import gettext_lazy as _
import os

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = '🧍 User Profile Section'
    fk_name = 'user'
    fieldsets = (
        (None, {
            'fields': ('address', 'profile_picture', 'date_of_birth', 'gender'),
            'description': "This section contains the user's personal profile data.",
        }),
    )

class UserAddressInline(admin.StackedInline):
    model = UserAddress
    extra = 0
    verbose_name_plural = 'User Address Section'
    fk_name = 'user'
    fieldsets = (
        (None, {
            'fields': ('street_address', 'city', 'state', 'postal_code', 'country', 'is_primary'),
            'description': "Addresses associated with the user.",
        }),
    )

@admin.register(SystemUser)
class UserAdmin(BaseUserAdmin):
    inlines = [UserProfileInline, UserAddressInline]
    list_display = ('email', 'first_name', 'last_name', 'is_active', 'is_staff')
    list_filter = ('is_active', 'is_staff', 'profile__gender')
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        (_('Personal info'), {
            'fields': ('first_name', 'last_name', 'phone_number', 'brand_name', 'licence')
        }),
        (_('Permissions'), {
            'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')
        }),
        (_('Important dates'), {'fields': ('last_login',)}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2'),
        }),
    )
    search_fields = ('email', 'first_name', 'last_name')
    ordering = ('email',)
    filter_horizontal = ('groups', 'user_permissions',)

@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('user', 'date_of_birth', 'gender')
    search_fields = ('user__email',)

@admin.register(UserAddress)
class UserAddressAdmin(admin.ModelAdmin):
    list_display = ('user', 'street_address', 'city', 'state', 'postal_code', 'country', 'is_primary')
    search_fields = ('user__email', 'city', 'state', 'country')
    list_filter = ('is_primary', 'country')
