from unittest.mock import patch

from odoo.tests import tagged
from odoo.addons.base.tests.common import HttpCaseWithUserPortal


@tagged('post_install', '-at_install')
class TestSaleExternalTaxesSalePortal(HttpCaseWithUserPortal):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.fp_external = cls.env['account.fiscal.position'].create({
            'name': 'External',
        })
        cls.product_test = cls.env['product.product'].create({
            'name': 'Test product',
            'list_price': 10.00,
            'standard_price': 10.00,
            'supplier_taxes_id': None,
        })

    def _create_sale_order(self, partner):
        return self.env['sale.order'].create({
            'name': 'test',
            'partner_id': partner.id,
            'fiscal_position_id': self.fp_external.id,
            'date_order': '2023-01-01',
            'order_line': [
                (0, 0, {
                    'product_id': self.product_test.id,
                    'tax_id': None,
                    'price_unit': self.product_test.list_price,
                }),
            ],
            'sale_order_option_ids': [
                (0, 0, {
                    'name': 'optional product',
                    'price_unit': 1,
                    'uom_id': self.env.ref('uom.product_uom_unit').id,
                    'product_id': self.env['product.product'].create({'name': 'optional product'}).id,
                }),
            ],
        })

    def test_01_portal_test_optional_products(self):
        sale_management = self.env['ir.module.module']._get('sale_management')
        if sale_management.state != 'installed':
            self.skipTest("sale_management module is not installed")

        self.env['product.pricelist'].search([]).unlink()
        portal_partner = self.env['res.users'].sudo().search([('login', '=', 'portal')]).partner_id
        portal_partner.write({
            'state_id': self.env.ref('base.state_us_25').id,
            'zip': '07002',
            'country_id': self.env.ref('base.us').id,
        })
        order = self._create_sale_order(portal_partner)

        self.env.ref('sale.sale_order_comp_rule').active = False
        self.env.ref('sale.sale_order_line_comp_rule').active = False

        order.action_quotation_sent()

        mixin_path = 'odoo.addons.odex30_account_external_tax.models.account_external_tax_mixin.AccountExternalTaxMixin'
        with patch(f'{mixin_path}._get_and_set_external_taxes_on_eligible_records') as mocked_get_and_set, \
             patch(f'{mixin_path}._compute_is_tax_computed_externally', lambda self: self.write({'is_tax_computed_externally': True})):
            self.start_tour('/', 'sale_external_optional_products', login='portal')
            mocked_get_and_set.assert_called()


            self.assertEqual(mocked_get_and_set.call_count, 4, 'External taxes were not calculated enough times during this tour.')
