from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy as _
from .models import CustomUser, OTPVerification


@admin.register(CustomUser)
class CustomUserAdmin(UserAdmin):
    list_display = ('phone_number', 'email', 'first_name', 'last_name', 'role', 'status', 'is_active')
    list_filter = ('role', 'status', 'is_active', 'is_staff', 'is_superuser')
    search_fields = ('phone_number', 'email', 'first_name', 'last_name', 'id_number')
    ordering = ('-date_joined',)
    
    fieldsets = (
        (None, {'fields': ('phone_number', 'email', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'username')}),
        (_('KYC Data'), {
            'fields': (
                'id_number', 'kra_pin', 'date_of_birth', 'gender', 'nationality',
                'marital_status', 'address', 'city', 'county', 'postal_code'
            ),
        }),
        (_('Business Info'), {
            'fields': (
                'employer', 'business_name', 'business_type', 'monthly_income'
            ),
        }),
        (_('Emergency Contact'), {
            'fields': (
                'emergency_contact_name', 'emergency_contact_phone',
                'emergency_contact_relationship'
            ),
        }),
        (_('Documents'), {
            'fields': (
                'id_document', 'selfie', 'utility_bill', 'crb_report',
                'bank_statement'
            ),
        }),
        (_('Permissions'), {
            'fields': (
                'role', 'status', 'is_active', 'is_staff', 'is_superuser',
                'groups', 'user_permissions'
            ),
        }),
        (_('Verification'), {
            'fields': ('is_phone_verified', 'is_email_verified'),
        }),
        (_('Important dates'), {
            'fields': ('last_login', 'date_joined', 'last_login_at'),
        }),
    )
    
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('phone_number', 'email', 'password1', 'password2'),
        }),
        (_('Personal info'), {
            'fields': ('first_name', 'last_name', 'username'),
        }),
        (_('Role & Permissions'), {
            'fields': ('role', 'is_active', 'is_staff', 'is_superuser'),
        }),
    )


@admin.register(OTPVerification)
class OTPVerificationAdmin(admin.ModelAdmin):
    list_display = ('user', 'otp_code', 'is_used', 'created_at', 'expires_at')
    list_filter = ('is_used', 'created_at')
    search_fields = ('user__phone_number', 'user__email', 'otp_code')
