from odoo.tests.common import HttpCase, tagged


@tagged('post_install', '-at_install')
class TestQuickTemplateControllers(HttpCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.category = cls.env['mail.quick.template.category'].create({
            'name': 'Ctrl', 'sequence': 5,
        })
        cls.model_partner = cls.env['ir.model']._get('res.partner')
        cls.global_tmpl = cls.env['mail.quick.template'].create({
            'name': 'Global Ctrl',
            'category_id': cls.category.id,
            'body_html': '<p>Hi {{name}}</p>',
        })
        cls.partner_tmpl = cls.env['mail.quick.template'].create({
            'name': 'Partner Ctrl',
            'category_id': cls.category.id,
            'model_id': cls.model_partner.id,
            'body_html': '<p>Partner {{name}}</p>',
        })

    def _call(self, route, params):
        return self.make_jsonrpc_request(route, params)

    def test_get_returns_global_and_model(self):
        self.authenticate('admin', 'admin')
        result = self._call('/do_mail_quick_templates/get', {
            'res_model': 'res.partner', 'res_id': False,
        })
        names = [
            t['name']
            for cat in result['categories']
            for t in cat['templates']
        ]
        self.assertIn('Global Ctrl', names)
        self.assertIn('Partner Ctrl', names)

    def test_get_hides_unrelated_model_template(self):
        self.authenticate('admin', 'admin')
        result = self._call('/do_mail_quick_templates/get', {
            'res_model': 'res.users', 'res_id': False,
        })
        names = [
            t['name']
            for cat in result['categories']
            for t in cat['templates']
        ]
        self.assertIn('Global Ctrl', names)
        self.assertNotIn('Partner Ctrl', names)

    def test_tn08_unknown_model_graceful(self):
        self.authenticate('admin', 'admin')
        result = self._call('/do_mail_quick_templates/get', {
            'res_model': 'no.such.model', 'res_id': False,
        })
        self.assertIn('categories', result)

    def test_preview_resolves(self):
        partner = self.env['res.partner'].create({'name': 'Zeta'})
        self.authenticate('admin', 'admin')
        result = self._call('/do_mail_quick_templates/preview', {
            'template_id': self.partner_tmpl.id,
            'res_model': 'res.partner',
            'res_id': partner.id,
        })
        self.assertIn('Zeta', result['resolved_html'])

    def test_log_use_creates_log(self):
        self.authenticate('admin', 'admin')
        result = self._call('/do_mail_quick_templates/log_use', {
            'template_id': self.global_tmpl.id,
            'res_model': 'res.partner',
            'res_id': 1,
        })
        self.assertTrue(result['success'])
        self.assertTrue(self.env['mail.quick.template.log'].search([
            ('template_id', '=', self.global_tmpl.id),
        ]))

    def test_toggle_favorite_roundtrip(self):
        self.authenticate('admin', 'admin')
        r1 = self._call('/do_mail_quick_templates/toggle_favorite', {
            'template_id': self.global_tmpl.id,
        })
        self.assertTrue(r1['is_favorite'])
        r2 = self._call('/do_mail_quick_templates/toggle_favorite', {
            'template_id': self.global_tmpl.id,
        })
        self.assertFalse(r2['is_favorite'])
