"""
Menus et permissions du module restaurant.

Ces definitions sont injectees dans le moteur central
(`facturation.menu_permissions`) : la sidebar, la matrice « Permissions
menus » et le middleware de protection des URLs les traitent comme les autres
modules. Ce fichier ne doit rien importer de facturation (import circulaire).
"""

from django.utils.translation import gettext_lazy as _

SECTION_KEY = "restaurant"

RESTAURANT_MENU_SECTION = {
    "key": SECTION_KEY, "label": _("Restaurant"), "icon": "bi bi-cup-hot", "is_collapsible": True,
}


def _item(key, label, icon, url_name, prefixes, permission_key=None):
    item = {
        "key": key, "label": label, "icon": icon, "section": SECTION_KEY,
        "url_name": f"restaurant:{url_name}", "active_path_prefixes": prefixes, "configurable": True,
    }
    if permission_key:
        item["permission_key"] = permission_key
    return item


RESTAURANT_MENU_ITEMS = (
    _item("restaurant_commandes", _("Commandes en cours"), "bi bi-journal-check", "commandes", ("/restaurant/commandes/",)),
    _item("restaurant_kds", _("Écran cuisine / bar"), "bi bi-display", "kds-choix", ("/restaurant/cuisine/",)),
    _item("restaurant_reservations", _("Réservations"), "bi bi-calendar-check", "reservations", ("/restaurant/reservations/",)),
    _item("restaurant_rapports", _("Rapports restaurant"), "bi bi-graph-up-arrow", "rapports", ("/restaurant/rapports/",)),
    _item("restaurant_impressions", _("File d'impression"), "bi bi-printer", "impressions", ("/restaurant/impressions/",),
          permission_key="restaurant_reimprimer"),
    _item("restaurant_config", _("Configuration restaurant"), "bi bi-sliders", "configuration", ("/restaurant/configuration/",)),
)


def _perm(key, module, label):
    return {"key": key, "section": SECTION_KEY, "module_label": module, "label": label}


_CMD = _("Restaurant - Commandes")
_KDS = _("Restaurant - Cuisine / bar")
_RESA = _("Restaurant - Réservations")
_RAPPORTS = _("Restaurant - Rapports")
_CONFIG = _("Restaurant - Configuration")

RESTAURANT_PERMISSION_DEFINITIONS = (
    _perm("restaurant_commandes", _CMD, _("Voir les commandes en attente de tout le restaurant")),
    _perm("restaurant_addition", _CMD, _("Imprimer l'addition")),
    _perm("restaurant_annuler", _CMD, _("Annuler des articles déjà envoyés / une commande")),
    _perm("restaurant_reimprimer", _CMD, _("Réimprimer les bons et gérer la file d'impression")),
    _perm("restaurant_kds", _KDS, _("Écran cuisine / bar")),
    _perm("restaurant_reservations", _RESA, _("Consulter les réservations")),
    _perm("restaurant_reservations_gerer", _RESA, _("Créer / modifier les réservations")),
    _perm("restaurant_rapports", _RAPPORTS, _("Rapports restaurant")),
    _perm("restaurant_rapports_export", _RAPPORTS, _("Exporter les rapports restaurant")),
    _perm("restaurant_config", _CONFIG, _("Consulter la configuration")),
    _perm("restaurant_config_update", _CONFIG, _("Modifier tables, destinations, imprimantes et agents")),
)

RESTAURANT_PERMISSION_KEYS = frozenset(item["key"] for item in RESTAURANT_PERMISSION_DEFINITIONS)

# Droits par defaut des profils restaurant (modifiables ensuite dans la matrice).
_SERVEUR = {"pos", "restaurant_commandes", "restaurant_addition", "restaurant_reservations"}
_CAISSIER = _SERVEUR | {
    "restaurant_reimprimer", "restaurant_reservations_gerer", "ventes_factures", "clients", "clients_create",
    "rapport_journalier",
}
_CUISINE = {"restaurant_kds", "restaurant_reimprimer"}
_GERANT = (RESTAURANT_PERMISSION_KEYS - {"restaurant_config_update"}) | {
    "dashboard", "pos", "produits", "ventes_factures", "ventes_factures_export", "clients", "clients_create",
    "stocks", "rapport_journalier", "rapport_mensuel", "rapport_chiffre_affaires", "rapport_vendeurs", "rapports_export",
}

RESTAURANT_ROLE_DEFAULT_PERMISSION_KEYS = {
    "SERVEUR": frozenset(_SERVEUR),
    "CAISSIER": frozenset(_CAISSIER),
    "CUISINIER": frozenset(_CUISINE),
    "BARMAN": frozenset(_CUISINE),
    "GERANT": frozenset(_GERANT),
}

# Destination d'atterrissage d'un profil restaurant sans tableau de bord.
RESTAURANT_FIRST_ALLOWED_MENU_KEYS = ("restaurant_kds", "restaurant_commandes", "restaurant_reservations", "restaurant_rapports")

# Protection des URLs par le middleware central (en plus des vues elles-memes).
RESTAURANT_VIEW_PERMISSION_KEYS = {
    "restaurant:commandes": "restaurant_commandes",
    "restaurant:kds-choix": "restaurant_kds",
    "restaurant:kds": "restaurant_kds",
    "restaurant:reservations": "restaurant_reservations",
    "restaurant:rapports": "restaurant_rapports",
    "restaurant:rapports-export": "restaurant_rapports_export",
    "restaurant:impressions": "restaurant_reimprimer",
    "restaurant:configuration": "restaurant_config",
    "restaurant:destinations": "restaurant_config",
}
