from django.contrib import admin
from django.contrib.admin import SimpleListFilter
from django.utils.translation import gettext_lazy as _
from bigsansar.contrib.sites.forms import create_domainform, customaddpageform, custompageform
from bigsansar.contrib.sites.models import domains, pages
from django.contrib.auth.models import User


# Register your models here.


class domain_filter(SimpleListFilter):
    title = _('Domains')  # a label for our filter
    parameter_name = "domain"  # you can put anything here

    def lookups(self, request, model_admin):
        # This is where you create filter options; we have two:
        qs = model_admin.get_queryset(request)
        return qs.values_list('domain_id', 'domain__domain').order_by('domain').distinct()

    def queryset(self, request, queryset):

        # This is where you process parameters selected by use via filter options:
        if self.value():
            return queryset.filter(domain=self.value())


class domainadmin(admin.ModelAdmin):
    form = create_domainform
    list_display = ['domain', 'publish_date', 'visitor']
    search_fields = ['domain']

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(user=request.user)
    
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        qs = super().formfield_for_foreignkey(db_field, request, **kwargs)
        if db_field.name == "user":
            if request.user.is_superuser:
                return qs
            kwargs["queryset"] = User.objects.filter(username=request.user.username)
        return super().formfield_for_foreignkey(db_field, request, **kwargs)


class pageadmin(admin.ModelAdmin):
    add_form = customaddpageform
    form = custompageform
    prepopulated_fields = {"slug": ("title",)}
    list_display = ['slug', 'domain', 'publish_date', 'visitor']
    list_filter = (domain_filter,)
    search_fields = ['slug']

    def get_form(self, request, obj=None, **kwargs):
        """
        Use special form during foo creation
        """
        defaults = {}
        if obj is None:
            defaults['form'] = self.add_form
        defaults.update(kwargs)
        return super().get_form(request, obj, **defaults)
        

admin.site.register(domains, domainadmin)
admin.site.register(pages, pageadmin)