from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _


def user_has_group_or_permission(user, permission):
    if user.is_superuser:
        return True

    group_names = user.groups.values_list("name", flat=True)
    if not group_names:
        return True

    return user.groups.filter(permissions__codename=permission).exists()


PAGES = [
    {
        "seperator": True,
        "items": [
            {
                "title": _("Bosh sahifa"),
                "icon": "home",
                "link": reverse_lazy("admin:index"),
            },
        ],
    },
    {
        "seperator": True,
        "title": _("Foydalanuvchilar"),
        "items": [
            {
                "title": _("Guruhlar"),
                "icon": "person_add",
                "link": reverse_lazy("admin:auth_group_changelist"),
                "permission": lambda request: user_has_group_or_permission(
                    request.user, "view_group"
                ),
            },
            {
                "title": _("Foydalanuvchilar"),
                "icon": "person_add",
                "link": reverse_lazy("admin:auth_user_changelist"),
                "permission": lambda request: user_has_group_or_permission(
                    request.user, "view_user"
                ),
            },
        ],
    },
    {
        "seperator": True,
        "title": _("Geologiya"),
        "items": [
            {
                "title": _("Madanlar"),
                "icon": "hiking",
                "link": reverse_lazy("admin:geology_geology_changelist"),
                "permission": lambda request: user_has_group_or_permission(
                    request.user, "view_geology"
                ),
            },
            {
                "title": _("Galereya"),
                "icon": "gallery_thumbnail",
                "link": reverse_lazy("admin:geology_gallerycategory_changelist"),
                "permission": lambda request: user_has_group_or_permission(
                    request.user, "view_photo"
                ),
            },
        ],
    },
]
