from django.utils.translation import gettext_lazy as _

from allianceauth import hooks
from allianceauth.services.hooks import MenuItemHook, UrlHook

from .urls import backoffice as backoffice_urls
from .urls import storefront as storefront_urls


class ShopMenuItem(MenuItemHook):
    """Sidebar entry into the back-office for authorized users."""

    def __init__(self):
        MenuItemHook.__init__(
            self,
            _("Shop"),
            "fas fa-store fa-fw",
            "backoffice:index",
            navactive=["backoffice:"],
        )

    def render(self, request):
        if request.user.has_perm("shop.basic_access"):
            return MenuItemHook.render(self, request)
        return ""


@hooks.register("menu_item_hook")
def register_menu():
    return ShopMenuItem()


@hooks.register("url_hook")
def register_backoffice_urls():
    return UrlHook(backoffice_urls, "backoffice", r"^shop-backoffice/")


@hooks.register("url_hook")
def register_storefront_urls():
    return UrlHook(
        storefront_urls,
        "storefront",
        r"^shop/",
        excluded_views=[
            "shop.views.storefront.detail",
            "shop.views.storefront.item_detail",
        ],
    )
