from odoo import fields, models, api

class ResUsers(models.Model):
    _inherit = 'res.users'

    @api.model_create_multi
    def create(self, vals_list):
        users = super().create(vals_list)
        for user in users:
            self._check_and_set_executor_home(user)
        return users

    def write(self, vals):
        res = super().write(vals)
        if 'group_ids' in vals:
            for user in self:
                self._check_and_set_executor_home(user)
        return res

    def _check_and_set_executor_home(self, user):
        executor_group = self.env.ref('directive_layer.role_directive_executor_res_groups', raise_if_not_found=False)
        dashboard_action = self.env.ref('directive_layer.action_executor_dashboard_client', raise_if_not_found=False)

        if executor_group and dashboard_action:
            if user.has_group('directive_layer.role_directive_executor_res_groups'):
                if user.action_id != dashboard_action:
                    user.sudo().write({'action_id': dashboard_action.id})