# Fusion Accounting - Menu Visibility Rules
# Restricts certain accounting menus to users with the account readonly group

from odoo import models


class FusionIrUiMenu(models.Model):
    """Controls visibility of advanced accounting menus so they are
    only shown to users who have at least the account-readonly role."""

    _inherit = 'ir.ui.menu'

    def _visible_menu_ids(self, debug=False):
        """Filter out specialised accounting menus for users lacking
        the ``account.group_account_readonly`` permission."""
        visible = super()._visible_menu_ids(debug)

        if not self.env.user.has_group('account.group_account_readonly'):
            restricted_refs = [
                'fusion_accounting.account_tag_menu',
                'fusion_accounting.menu_account_group',
                'fusion_accounting.menu_action_account_report_multicurrency_revaluation',
            ]
            hidden_ids = set()
            for xml_ref in restricted_refs:
                menu = self.env.ref(xml_ref, raise_if_not_found=False)
                if menu:
                    hidden_ids.add(menu.sudo().id)
            return visible - hidden_ids

        return visible
