from django.urls import path, reverse_lazy
from django.utils.translation import gettext_lazy as _
from wagtail import hooks
from wagtail.admin.auth import user_passes_test
from wagtail.admin.menu import MenuItem
from wagtail.snippets.models import register_snippet

from .admin import event_sidebar_viewset, event_explorer_viewset
from .merge_organizers import MergeOrganizersConfirmView, MergeOrganizersSelectView
from .views import EventOrganizerSnippetViewSet, event_organizer_chooser_viewset

register_snippet(EventOrganizerSnippetViewSet)

superuser_only = user_passes_test(lambda user: user.is_superuser)


class SuperuserMenuItem(MenuItem):
    def is_shown(self, request):
        return request.user.is_superuser


@hooks.register("register_admin_urls")
def register_merge_organizers_urls():
    return [
        path(
            "merge-organizers/",
            superuser_only(MergeOrganizersSelectView.as_view()),
            name="events_merge_organizers",
        ),
        path(
            "merge-organizers/confirm/",
            superuser_only(MergeOrganizersConfirmView.as_view()),
            name="events_merge_organizers_confirm",
        ),
    ]


@hooks.register("register_settings_menu_item")
def register_merge_organizers_menu_item():
    return SuperuserMenuItem(
        _("Merge organizers"),
        reverse_lazy("events_merge_organizers"),
        icon_name="group",
        order=1100,
    )


@hooks.register("register_admin_viewset")
def register_viewset():
    return event_organizer_chooser_viewset


@hooks.register("register_admin_viewset")
def register_event_sidebar_viewset():
    return event_sidebar_viewset


@hooks.register("register_admin_viewset")
def register_event_explorer_viewset():
    return event_explorer_viewset
