from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import User

@admin.register(User)
class UserAdmin(BaseUserAdmin):
    list_display = ["username", "email", "neighborhood", "is_verified", "verification_status", "is_staff", "is_active", "date_joined"]
    list_filter = ["is_verified", "verification_status", "is_staff", "is_active", "neighborhood"]
    search_fields = ["username", "email", "phone"]
    ordering = ["-date_joined"]
    readonly_fields = ["date_joined", "last_seen", "verified_at"]
    fieldsets = (
        (None, {"fields": ("email", "username", "password")}),
        ("Personal", {"fields": ("bio", "phone", "profile_image", "neighborhood", "ward")}),
        ("Verification", {"fields": ("is_verified", "verification_status", "verified_at")}),
        ("Permissions", {"fields": ("is_active", "is_staff", "is_superuser", "groups", "user_permissions")}),
        ("Stats", {"fields": ("followers_count", "following_count", "date_joined", "last_seen")}),
    )
    actions = ["approve_verification", "reject_verification"]

    def approve_verification(self, request, queryset):
        from django.utils import timezone
        queryset.update(is_verified=True, verification_status="approved", verified_at=timezone.now())
        self.message_user(request, f"{queryset.count()} users verified.")
    approve_verification.short_description = "✅ Approve verification"

    def reject_verification(self, request, queryset):
        queryset.update(verification_status="rejected")
        self.message_user(request, f"{queryset.count()} applications rejected.")
    reject_verification.short_description = "❌ Reject verification"
