# -*- coding: utf-8 -*-
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.tests import tagged

from contextlib import contextmanager
from unittest.mock import patch


@tagged('post_install', '-at_install')
class TestAccountIncomingSupplierInvoice(AccountTestInvoicingCommon):

    @classmethod
    def setUpClass(cls, chart_template_ref=None):
        super().setUpClass(chart_template_ref=chart_template_ref)

        cls.internal_user = cls.env['res.users'].create({
            'name': 'Internal User',
            'login': 'internal.user@test.odoo.com',
            'email': 'internal.user@test.odoo.com',
        })

        cls.supplier_partner = cls.env['res.partner'].create({
            'name': 'Your Supplier',
            'email': 'supplier@other.company.com',
            'supplier_rank': 10,
        })

        cls.journal = cls.company_data['default_journal_purchase']
        cls.attachment_number = 0

    def _create_dummy_pdf_attachment(self):
        self.attachment_number += 1
        rawpdf_base64 = 'JVBERi0xLjYNJeLjz9MNCjI0IDAgb2JqDTw8L0ZpbHRlci9GbGF0ZURlY29kZS9GaXJzdCA0L0xlbmd0aCAyMTYvTiAxL1R5cGUvT2JqU3RtPj5zdHJlYW0NCmjePI9RS8MwFIX/yn1bi9jepCQ6GYNpFBTEMsW97CVLbjWYNpImmz/fVsXXcw/f/c4SEFarepPTe4iFok8dU09DgtDBQx6TMwT74vaLTE7uSPDUdXM0Xe/73r1FnVwYYEtHR6d9WdY3kX4ipRMV6oojSmxQMoGyac5RLBAXf63p38aGA7XPorLewyvFcYaJile8rB+D/YcwiRdMMGScszO8/IW0MdhsaKKYGA46gXKTr/cUQVY4We/cYMNpnLVeXPJUXHs9fECr7kAFk+eZ5Xr9LcAAfKpQrA0KZW5kc3RyZWFtDWVuZG9iag0yNSAwIG9iag08PC9GaWx0ZXIvRmxhdGVEZWNvZGUvRmlyc3QgNC9MZW5ndGggNDkvTiAxL1R5cGUvT2JqU3RtPj5zdHJlYW0NCmjeslAwULCx0XfOL80rUTDU985MKY42NAIKBsXqh1QWpOoHJKanFtvZAQQYAN/6C60NCmVuZHN0cmVhbQ1lbmRvYmoNMjYgMCBvYmoNPDw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].create({
            'name': f"attachment_{self.attachment_number}",
            'datas': rawpdf_base64,
            'type': 'binary',
            'mimetype': 'application/pdf',
        })

    def _create_dummy_xml_attachment(self):
        self.attachment_number += 1
        return self.env['ir.attachment'].create({
            'name': f"attachment_{self.attachment_number}",
            'raw': '<test/>',
            'mimetype': 'application/xml',
        })

    def _create_dummy_gif_attachment(self):
        self.attachment_number += 1
        return self.env['ir.attachment'].create({
            'name': f"attachment_{self.attachment_number}",
            'datas': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=",
            'mimetype': 'image/gif',
        })

    @contextmanager
    def with_success_decoder(self, omit=None):
        decoded_files = set()

        def get_edi_decoder(_record, file_data, new=False):

            def decoder(*args, **kwargs):
                return not omit or file_data['attachment'].name not in omit

            if decoder():
                decoded_files.add(file_data['filename'])
            return decoder

        with patch.object(type(self.env['account.move']), '_get_edi_decoder', get_edi_decoder):
            yield decoded_files

    @contextmanager
    def with_simulated_embedded_xml(self, pdf):
        super_decode_edi_pdf = type(self.env['ir.attachment'])._decode_edi_pdf
        xml_filename = f"{pdf.name}_xml"

        def decode_edi_pdf(record, filename, content):
            results = super_decode_edi_pdf(record, filename, content)
            if filename == pdf.name:
                embedded_files = self.env['ir.attachment']._decode_edi_xml(xml_filename, '<test></test>')
                for file_data in embedded_files:
                    file_data['sort_weight'] += 1
                    file_data['originator_pdf'] = pdf
                results += embedded_files
            return results

        with patch.object(type(self.env['ir.attachment']), '_decode_edi_pdf', decode_edi_pdf):
            yield xml_filename

    def _assert_extend_with_attachments(self, input_values, expected_values=None, new=False, **context):
        if not expected_values:
            expected_values = input_values
        attachments = self.env['ir.attachment'].browse([x.id for x in input_values])
        nb_moves_before = self.env['account.move'].search_count([('company_id', '=', self.env.company.id)])
        results = self.env['account.move']\
            .with_context(**context, default_move_type='out_invoice', default_journal_id=self.company_data['default_journal_sale'].id)\
            ._extend_with_attachments(attachments, new=new)
        invoice_number = 0
        previous_invoice = None
        current_values = {}
        for attachment, invoice in results.items():
            if previous_invoice != invoice:
                invoice_number += 1
                previous_invoice = invoice

            current_values[attachment.name] = invoice_number

        self.assertEqual(current_values, {k.name: v for k, v in expected_values.items()})

        nb_moves_after = self.env['account.move'].search_count([('company_id', '=', self.env.company.id)])
        self.assertEqual(nb_moves_before + invoice_number, nb_moves_after)

    def test_supplier_invoice_mailed_from_supplier(self):
        message_parsed = {
            'message_id': 'message-id-dead-beef',
            'subject': 'Incoming bill',
            'from': '%s <%s>' % (self.supplier_partner.name, self.supplier_partner.email),
            'to': '%s@%s' % (self.journal.alias_id.alias_name, self.journal.alias_id.alias_domain),
            'body': "You know, that thing that you bought.",
            'attachments': [b'Hello, invoice'],
        }

        invoice = self.env['account.move'].message_new(message_parsed, {'move_type': 'in_invoice', 'journal_id': self.journal.id})

        message_ids = invoice.message_ids
        self.assertEqual(len(message_ids), 1, 'Only one message should be posted in the chatter')
        self.assertEqual(message_ids.body, '<p>Vendor Bill Created</p>', 'Only the invoice creation should be posted')

        following_partners = invoice.message_follower_ids.mapped('partner_id')
        self.assertEqual(following_partners, self.env.user.partner_id)
        self.assertRegex(invoice.name, r'BILL/\d{4}/\d{2}/0001')

    def test_supplier_invoice_forwarded_by_internal_user_without_supplier(self):
        """ In this test, the bill was forwarded by an employee,
            but no partner email address is found in the body."""
        message_parsed = {
            'message_id': 'message-id-dead-beef',
            'subject': 'Incoming bill',
            'from': '%s <%s>' % (self.internal_user.name, self.internal_user.email),
            'to': '%s@%s' % (self.journal.alias_id.alias_name, self.journal.alias_id.alias_domain),
            'body': "You know, that thing that you bought.",
            'attachments': [b'Hello, invoice'],
        }

        invoice = self.env['account.move'].message_new(message_parsed, {'move_type': 'in_invoice', 'journal_id': self.journal.id})

        message_ids = invoice.message_ids
        self.assertEqual(len(message_ids), 1, 'Only one message should be posted in the chatter')
        self.assertEqual(message_ids.body, '<p>Vendor Bill Created</p>', 'Only the invoice creation should be posted')

        following_partners = invoice.message_follower_ids.mapped('partner_id')
        self.assertEqual(following_partners, self.env.user.partner_id | self.internal_user.partner_id)

    def test_supplier_invoice_forwarded_by_internal_with_supplier_in_body(self):
        """ In this test, the bill was forwarded by an employee,
            and the partner email address is found in the body."""
        message_parsed = {
            'message_id': 'message-id-dead-beef',
            'subject': 'Incoming bill',
            'from': '%s <%s>' % (self.internal_user.name, self.internal_user.email),
            'to': '%s@%s' % (self.journal.alias_id.alias_name, self.journal.alias_id.alias_domain),
            'body': "Mail sent by %s <%s>:\nYou know, that thing that you bought." % (self.supplier_partner.name, self.supplier_partner.email),
            'attachments': [b'Hello, invoice'],
        }

        invoice = self.env['account.move'].message_new(message_parsed, {'move_type': 'in_invoice', 'journal_id': self.journal.id})

        message_ids = invoice.message_ids
        self.assertEqual(len(message_ids), 1, 'Only one message should be posted in the chatter')
        self.assertEqual(message_ids.body, '<p>Vendor Bill Created</p>', 'Only the invoice creation should be posted')

        following_partners = invoice.message_follower_ids.mapped('partner_id')
        self.assertEqual(following_partners, self.env.user.partner_id | self.internal_user.partner_id)

    def test_supplier_invoice_forwarded_by_internal_with_internal_in_body(self):
        """ In this test, the bill was forwarded by an employee,
            and the internal user email address is found in the body."""
        message_parsed = {
            'message_id': 'message-id-dead-beef',
            'subject': 'Incoming bill',
            'from': '%s <%s>' % (self.internal_user.name, self.internal_user.email),
            'to': '%s@%s' % (self.journal.alias_id.alias_name, self.journal.alias_id.alias_domain),
            'body': "Mail sent by %s <%s>:\nYou know, that thing that you bought." % (self.internal_user.name, self.internal_user.email),
            'attachments': [b'Hello, invoice'],
        }

        invoice = self.env['account.move'].message_new(message_parsed, {'move_type': 'in_invoice', 'journal_id': self.journal.id})

        message_ids = invoice.message_ids
        self.assertEqual(len(message_ids), 1, 'Only one message should be posted in the chatter')
        self.assertEqual(message_ids.body, '<p>Vendor Bill Created</p>', 'Only the invoice creation should be posted')

        following_partners = invoice.message_follower_ids.mapped('partner_id')
        self.assertEqual(following_partners, self.env.user.partner_id | self.internal_user.partner_id)

    def test_extend_with_attachments_multi_pdf(self):
        pdf1 = self._create_dummy_pdf_attachment()
        pdf2 = self._create_dummy_pdf_attachment()
        gif1 = self._create_dummy_gif_attachment()
        gif2 = self._create_dummy_gif_attachment()
        xml1 = self._create_dummy_xml_attachment()
        xml2 = self._create_dummy_xml_attachment()
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 1}, new=False)
            self.assertEqual(decoded_files, {pdf1.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 2}, new=True)
            self.assertEqual(decoded_files, {pdf1.name, pdf2.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 2}, new=True, from_alias=True)
            self.assertEqual(decoded_files, {pdf1.name, pdf2.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 1, gif1: 1, gif2: 1}, new=False)
            self.assertEqual(decoded_files, {pdf1.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 2, gif1: 3, gif2: 4}, new=True)
            self.assertEqual(decoded_files, {pdf1.name, pdf2.name, gif1.name, gif2.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 2, gif1: 3, gif2: 4}, expected_values={pdf1: 1, pdf2: 2}, new=True, from_alias=True)
            self.assertEqual(decoded_files, {pdf1.name, pdf2.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, xml1: 1}, new=False)
            self.assertEqual(decoded_files, {xml1.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, xml1: 1}, new=True)
            self.assertEqual(decoded_files, {xml1.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, xml1: 1}, new=True, from_alias=True)
            self.assertEqual(decoded_files, {xml1.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({xml1: 1, xml2: 1}, new=False)
            self.assertEqual(decoded_files, {xml1.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({xml1: 1, xml2: 2}, new=True)
            self.assertEqual(decoded_files, {xml1.name, xml2.name})
        with self.with_success_decoder() as decoded_files:
            self._assert_extend_with_attachments({xml1: 1, xml2: 2}, new=True, from_alias=True)
            self.assertEqual(decoded_files, {xml1.name, xml2.name})
        with self.with_success_decoder(omit={pdf1.name}) as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 2}, new=True)
            self.assertEqual(decoded_files, {pdf2.name})
        with self.with_success_decoder(omit={pdf1.name}) as decoded_files:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 2}, new=True, from_alias=True)
            self.assertEqual(decoded_files, {pdf2.name})
        with self.with_success_decoder() as decoded_files, self.with_simulated_embedded_xml(pdf1) as xml_filename:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 1}, new=False)
            self.assertEqual(decoded_files, {xml_filename})
        with self.with_success_decoder() as decoded_files, self.with_simulated_embedded_xml(pdf1) as xml_filename:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 2}, new=True)
            self.assertEqual(decoded_files, {xml_filename, pdf2.name})
        with self.with_success_decoder() as decoded_files, self.with_simulated_embedded_xml(pdf1) as xml_filename:
            self._assert_extend_with_attachments({pdf1: 1, pdf2: 2}, new=True, from_alias=True)
            self.assertEqual(decoded_files, {xml_filename, pdf2.name})
        with self.with_success_decoder() as decoded_files, self.with_simulated_embedded_xml(pdf1):
            self._assert_extend_with_attachments({pdf1: 1, xml1: 1}, new=False)
            self.assertEqual(decoded_files, {xml1.name})
        with self.with_success_decoder() as decoded_files, self.with_simulated_embedded_xml(pdf1):
            self._assert_extend_with_attachments({pdf1: 1, xml1: 1}, new=True)
            self.assertEqual(decoded_files, {xml1.name})
        with self.with_success_decoder() as decoded_files, self.with_simulated_embedded_xml(pdf1):
            self._assert_extend_with_attachments({pdf1: 1, xml1: 1}, new=True, from_alias=True)
            self.assertEqual(decoded_files, {xml1.name})
