# Copyright 2026 Criptomart
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase


class TestProductOriginChar(TransactionCase):
    """Test cases for product_origin_char module.

    This module adds a simple origin_text field to product.template
    with a related field in product.product for variant access.
    """

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))

        # Create test product template
        cls.product_tmpl = cls.env["product.template"].create(
            {
                "name": "Test Product",
                "type": "consu",
            }
        )
        cls.product = cls.product_tmpl.product_variant_ids[0]

    def test_01_origin_text_in_template(self):
        """Test that origin_text can be set in product.template"""
        self.product_tmpl.origin_text = "Valencia, Spain"
        self.assertEqual(self.product_tmpl.origin_text, "Valencia, Spain")

    def test_02_origin_text_in_product_related(self):
        """Test that product.product has related access to origin_text"""
        self.product_tmpl.origin_text = "Aragón, Spain"
        # Variant should show template's origin via related field
        self.assertEqual(self.product.origin_text, "Aragón, Spain")

    def test_03_origin_text_write_from_variant(self):
        """Test that origin_text can be written from variant (readonly=False)"""
        self.product.origin_text = "Basque Country"
        # Should update the template
        self.assertEqual(self.product_tmpl.origin_text, "Basque Country")

    def test_04_empty_origin(self):
        """Test that origin_text defaults to False/empty"""
        new_product = self.env["product.product"].create(
            {
                "name": "Product Without Origin",
                "type": "consu",
            }
        )
        self.assertFalse(new_product.origin_text)
        self.assertFalse(new_product.product_tmpl_id.origin_text)

    def test_05_multiple_products_independent_origins(self):
        """Test that different products have independent origins"""
        product2_tmpl = self.env["product.template"].create(
            {
                "name": "Test Product 2",
                "type": "consu",
            }
        )

        self.product_tmpl.origin_text = "Valencia, Spain"
        product2_tmpl.origin_text = "Aragón, Spain"

        self.assertEqual(self.product_tmpl.origin_text, "Valencia, Spain")
        self.assertEqual(product2_tmpl.origin_text, "Aragón, Spain")

    def test_06_product_variants_share_origin(self):
        """Test that all variants of a template share the same origin"""
        # Create product template with variants
        product_attr = self.env["product.attribute"].create({"name": "Color"})
        attr_value_red = self.env["product.attribute.value"].create(
            {"name": "Red", "attribute_id": product_attr.id}
        )
        attr_value_blue = self.env["product.attribute.value"].create(
            {"name": "Blue", "attribute_id": product_attr.id}
        )

        product_tmpl = self.env["product.template"].create(
            {
                "name": "Product with Variants",
                "type": "consu",
                "attribute_line_ids": [
                    (
                        0,
                        0,
                        {
                            "attribute_id": product_attr.id,
                            "value_ids": [
                                (6, 0, [attr_value_red.id, attr_value_blue.id])
                            ],
                        },
                    )
                ],
            }
        )

        # Set origin on template
        product_tmpl.origin_text = "Test Origin"

        # Verify all variants show the same origin
        for variant in product_tmpl.product_variant_ids:
            self.assertEqual(variant.origin_text, "Test Origin")

    def test_07_origin_update_propagates_to_variants(self):
        """Test that updating template origin updates all variants"""
        # Create product with variants
        product_attr = self.env["product.attribute"].create({"name": "Size"})
        attr_value_s = self.env["product.attribute.value"].create(
            {"name": "S", "attribute_id": product_attr.id}
        )
        attr_value_m = self.env["product.attribute.value"].create(
            {"name": "M", "attribute_id": product_attr.id}
        )

        product_tmpl = self.env["product.template"].create(
            {
                "name": "Sized Product",
                "type": "consu",
                "origin_text": "Original Origin",
                "attribute_line_ids": [
                    (
                        0,
                        0,
                        {
                            "attribute_id": product_attr.id,
                            "value_ids": [(6, 0, [attr_value_s.id, attr_value_m.id])],
                        },
                    )
                ],
            }
        )

        # Verify initial origin
        for variant in product_tmpl.product_variant_ids:
            self.assertEqual(variant.origin_text, "Original Origin")

        # Update origin on template
        product_tmpl.origin_text = "Updated Origin"

        # Verify all variants show updated origin
        for variant in product_tmpl.product_variant_ids:
            self.assertEqual(variant.origin_text, "Updated Origin")
