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

from odoo.tests import tagged
from odoo.tests.common import TransactionCase


@tagged("post_install", "-at_install")
class TestTheoreticalPriceCalculation(TransactionCase):
    """Test the theoretical price calculation to diagnose issues."""

    @classmethod
    def setUpClass(cls):
        super().setUpClass()

        # Create tax
        cls.tax = cls.env["account.tax"].create(
            {
                "name": "Test Tax 10%",
                "amount": 10.0,
                "amount_type": "percent",
                "type_tax_use": "sale",
            }
        )

        # Create pricelist with last_purchase_price base and 10% markup
        cls.pricelist = cls.env["product.pricelist"].create(
            {
                "name": "Test Auto Pricelist",
                "currency_id": cls.env.company.currency_id.id,
            }
        )

        cls.pricelist_item = cls.env["product.pricelist.item"].create(
            {
                "pricelist_id": cls.pricelist.id,
                "compute_price": "formula",
                "base": "last_purchase_price",
                "price_discount": -10.0,  # 10% markup (negative discount)
                "applied_on": "3_global",
            }
        )

        # Configure the pricelist in settings
        cls.env["ir.config_parameter"].sudo().set_param(
            "product_sale_price_from_pricelist.product_pricelist_automatic",
            str(cls.pricelist.id),
        )

        # Create product.product directly
        cls.product = cls.env["product.product"].create(
            {
                "name": "Test Product Direct",
                "list_price": 10.0,
                "standard_price": 5.0,
                "taxes_id": [(6, 0, [cls.tax.id])],
            }
        )

    def test_write_last_purchase_price_on_product(self):
        """Test that writing last_purchase_price_received on product.product works."""
        # Write directly to product.product
        self.product.write({"last_purchase_price_received": 100.0})

        # Re-read from DB to ensure value is persisted
        self.product.invalidate_recordset()
        self.assertEqual(
            self.product.last_purchase_price_received,
            100.0,
            "last_purchase_price_received should be 100.0 after write",
        )

    def test_compute_theoretical_price_from_product(self):
        """Test computing theoretical price when called on product.product."""
        # Set purchase price
        self.product.write(
            {
                "last_purchase_price_received": 100.0,
                "last_purchase_price_compute_type": "without_discounts",
            }
        )
        self.product.invalidate_recordset()

        # Verify price was written
        self.assertEqual(self.product.last_purchase_price_received, 100.0)

        # Compute theoretical price
        self.product._compute_theoritical_price()
        self.product.invalidate_recordset()

        # Expected: 100.0 * 1.10 = 110.0 (10% markup)
        self.assertAlmostEqual(
            self.product.list_price_theoritical,
            110.0,
            places=2,
            msg=f"Theoretical price should be 110.0, got {self.product.list_price_theoritical}",
        )

    def test_compute_theoretical_price_from_template(self):
        """Test computing theoretical price when called on product.template."""
        template = self.product.product_tmpl_id

        # Set purchase price on variant
        self.product.write(
            {
                "last_purchase_price_received": 100.0,
                "last_purchase_price_compute_type": "without_discounts",
            }
        )
        self.product.invalidate_recordset()

        # Verify price was written
        self.assertEqual(self.product.last_purchase_price_received, 100.0)

        # Compute theoretical price via template
        template._compute_theoritical_price()
        self.product.invalidate_recordset()

        # Expected: 100.0 * 1.10 = 110.0 (10% markup)
        self.assertAlmostEqual(
            self.product.list_price_theoritical,
            110.0,
            places=2,
            msg=f"Theoretical price should be 110.0, got {self.product.list_price_theoritical}",
        )

    def test_field_storage_location(self):
        """Test that fields are stored on product.product, not product.template."""
        # Check field definitions
        product_fields = self.env["product.product"]._fields
        template_fields = self.env["product.template"]._fields

        # These should be stored fields on product.product
        self.assertFalse(
            product_fields["last_purchase_price_received"].compute,
            "last_purchase_price_received should NOT be computed on product.product",
        )
        self.assertFalse(
            product_fields["list_price_theoritical"].compute,
            "list_price_theoritical should NOT be computed on product.product",
        )

        # These should be computed fields on product.template
        self.assertTrue(
            template_fields["last_purchase_price_received"].compute,
            "last_purchase_price_received should be computed on product.template",
        )
        self.assertTrue(
            template_fields["list_price_theoritical"].compute,
            "list_price_theoritical should be computed on product.template",
        )
