# users/admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import Users, Roles, User_Roles, Role_Permissions
from .new_forms import CustomUserCreationForm, CustomUserChangeForm


@admin.register(Users)
class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = Users

    list_display = ["email", "is_staff", "is_active"]
    ordering = ["email"]
    search_fields = ("email",)

    fieldsets = (
        (None, {"fields": ("email", "password")}),
        (
            "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", "is_staff", "is_superuser"),
            },
        ),
    )



@admin.register(Roles)
class RolesAdmin(admin.ModelAdmin):
    list_display = ["role_name", "description"]
    search_fields = ["role_name"]


@admin.register(User_Roles)
class UserRolesAdmin(admin.ModelAdmin):
    list_display = ["user", "role", "tenant", "assigned_at"]
    list_filter = ["role", "tenant"]
    search_fields = ["user__username", "role__role_name", "tenant__name"]


@admin.register(Role_Permissions)
class RolePermissionsAdmin(admin.ModelAdmin):
    list_display = ["role", "module_name", "action"]
    list_filter = ["role", "module_name", "action"]
    search_fields = ["role__role_name", "module_name", "action"]
