from django.contrib import admin
from django.contrib.auth import admin as auth_admin, get_user_model
from django.utils.translation import gettext_lazy as _

from vbv_lernwelt.core.models import Country, JobLog, Organisation
from vbv_lernwelt.core.utils import pretty_print_json

User = get_user_model()


class LogAdmin(admin.ModelAdmin):
    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def get_readonly_fields(self, request, obj=None):
        # set all fields read only
        return [field.name for field in self.opts.local_fields] + [
            field.name for field in self.opts.local_many_to_many
        ]

    def pretty_print_json(self, json_string):
        return pretty_print_json(json_string)


@admin.register(User)
class UserAdmin(auth_admin.UserAdmin):
    fieldsets = (
        (None, {"fields": ("username", "password")}),
        (_("Personal info"), {"fields": ("first_name", "last_name", "email")}),
        (
            _("Permissions"),
            {
                "fields": (
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "groups",
                    "user_permissions",
                ),
            },
        ),
        (_("Important dates"), {"fields": ("last_login", "date_joined")}),
        (
            _("Profile"),
            {
                "fields": (
                    "language",
                    "avatar",
                    "street",
                    "street_number",
                    "postal_code",
                    "city",
                    "country",
                    "invoice_address",
                )
            },
        ),
        (
            _("Organisation"),
            {
                "fields": (
                    "organisation",
                    "organisation_detail_name",
                    "organisation_street",
                    "organisation_street_number",
                    "organisation_postal_code",
                    "organisation_city",
                    "organisation_country",
                )
            },
        ),
        (_("Additional data"), {"fields": ("additional_json_data",)}),
    )
    list_display = [
        "username",
        "first_name",
        "last_name",
        "is_active",
        "is_superuser",
        "sso_id",
    ]
    search_fields = ["first_name", "last_name", "email", "username", "sso_id"]


@admin.register(JobLog)
class JobLogAdmin(LogAdmin):
    date_hierarchy = "started"
    list_display = (
        "job_name",
        "started",
        "ended",
        "runtime",
        "success",
    )
    list_filter = ["job_name", "success"]

    def json_data_pretty(self, instance):
        return self.pretty_print_json(instance.json_data)

    def get_readonly_fields(self, request, obj=None):
        return super().get_readonly_fields(request, obj) + ["json_data_pretty"]

    def runtime(self, obj):
        if obj.ended:
            return (obj.ended - obj.started).seconds // 60
        return None


@admin.register(Organisation)
class OrganisationAdmin(admin.ModelAdmin):
    list_display = (
        "organisation_id",
        "name_de",
        "name_fr",
        "name_it",
    )


@admin.register(Country)
class CountryAdmin(admin.ModelAdmin):
    list_display = (
        "order_id",
        "country_id",
        "name_de",
        "name_fr",
        "name_it",
    )
