from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import Group
from .models import User, RegistrationDomainRule, RegistrationRestrictionSetting, UserActivity

SESSION_MONITOR_GROUP = "Session Monitor"

# Ensure "Session Monitor" group exists in Django Admin
try:
    Group.objects.get_or_create(name=SESSION_MONITOR_GROUP)
except Exception:
    pass

@admin.register(User)
class UserAdmin(BaseUserAdmin):
    fieldsets = BaseUserAdmin.fieldsets + (
        ('Zusatzinfos', {'fields': ('display_name', 'skills', 'is_admin_user')}),
    )
    list_display = ['username', 'email', 'display_name', 'is_admin_user', 'is_staff', 'is_superuser']

@admin.register(RegistrationDomainRule)
class RegistrationDomainRuleAdmin(admin.ModelAdmin):
    list_display = ['domain', 'is_active', 'created_at']
    list_filter = ['is_active']
    search_fields = ['domain']

@admin.register(RegistrationRestrictionSetting)
class RegistrationRestrictionSettingAdmin(admin.ModelAdmin):
    list_display = ['is_restriction_enabled']

@admin.register(UserActivity)
class UserActivityAdmin(admin.ModelAdmin):
    list_display = ['user', 'guest_name', 'ip_address', 'last_path', 'last_seen']
    list_filter = ['last_seen']
    search_fields = ['ip_address', 'guest_name', 'user__username', 'user__email', 'user__display_name', 'last_path']
