from odoo.tests.common import tagged
from .common import TestAccountAvataxCommon


@tagged("-at_install", "post_install")
class TestAccountAvalaraVAT(TestAccountAvataxCommon):

    @classmethod
    def setUpClass(cls):
        res = super().setUpClass()
        cls.shipping_partner = cls.partner.copy({
            'name': 'Delivery Partner',
            'street': '1000 Market St',
        })
        cls.partner.vat = 'businessid'
        cls.product = cls.env["product.product"].create({
            'name': "Product",
            'list_price': 15.00,
            'standard_price': 15.00,
            'supplier_taxes_id': None,
            'avatax_category_id': cls.env.ref('account_avatax.DC010000').id,
        })
        with cls._capture_request(return_value={'lines': [], 'summary': []}) as capture:
            cls.invoice = cls.env['account.move'].create({
                'partner_id': cls.partner.id,
                'partner_shipping_id': cls.shipping_partner.id,
                'fiscal_position_id': cls.fp_avatax.id,
                'invoice_date': '2021-01-01',
                'move_type': 'out_invoice',
                'invoice_line_ids': [
                    (0, 0, {
                        'product_id': cls.product.id,
                        'price_unit': cls.product.list_price,
                    }),
                ]
            })
            cls.invoice.button_external_tax_calculation()
        cls.captured_arguments = capture.val['json']['createTransactionModel']
        return res

    def test_business_id(self):

        vat = self.captured_arguments['businessIdentificationNo']
        self.assertEqual(vat, 'businessid')

    def test_country_code(self):

        self.assertTrue(all(
            all(address.get('country'))
            for address in self.captured_arguments['addresses'].values()
        ))

    def test_currency_code(self):

        currency_code = self.captured_arguments['currencyCode']
        self.assertEqual(currency_code, 'USD')

    def test_ship_to_address(self):
        destination_address = self.captured_arguments['addresses']['shipTo']
        self.assertEqual(destination_address, {
            'city': 'San Francisco',
            'country': 'US',
            'line1': '1000 Market St',
            'postalCode': '94114',
            'region': 'CA',
        })

    def test_ship_from_address(self):
        country_code = self.captured_arguments['addresses']['shipFrom']['country']
        self.assertEqual(country_code, 'US')
