from odoo import api, fields, models


class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    enable_quick_templates = fields.Boolean(
        string='Enable Chatter Quick Reply Templates',
        config_parameter='do_mail_quick_templates.enabled',
        default=True,
        help="When disabled, the Templates button is hidden from every "
             "chatter compose bar.",
    )

    def set_values(self):
        """Mirror the global flag onto a company-specific key so the picker can
        honour per-company configuration (Section 2.9)."""
        super().set_values()
        icp = self.env['ir.config_parameter'].sudo()
        company_key = 'do_mail_quick_templates.enabled_%s' % self.env.company.id
        icp.set_param(company_key, str(bool(self.enable_quick_templates)))

    @api.model
    def get_values(self):
        res = super().get_values()
        enabled = self.env['mail.quick.template']._is_feature_enabled()
        res['enable_quick_templates'] = enabled
        return res
