# 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 TestSaleOrderUpdate(TransactionCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.partner = cls.env.ref("base.res_partner_1")

        cls.product = cls.env["product.product"].create(
            {
                "name": "Test Product SO",
                "list_price": 120.0,
                "standard_price": 80.0,
            }
        )

        cls.downpayment_product = cls.env["product.product"].create(
            {
                "name": "Downpayment Product",
                "type": "service",
                "list_price": 10.0,
            }
        )

    def _create_sale_order(self, values=None):
        vals = {
            "partner_id": self.partner.id,
            "order_line": [
                (
                    0,
                    0,
                    {
                        "product_id": self.product.id,
                        "product_uom": self.product.uom_id.id,
                        "product_uom_qty": 2.0,
                        "price_unit": 50.0,
                        "tax_id": [(6, 0, self.product.taxes_id.ids)],
                    },
                ),
                (0, 0, {"display_type": "line_note", "name": "Note"}),
                (
                    0,
                    0,
                    {
                        "product_id": self.downpayment_product.id,
                        "product_uom": self.downpayment_product.uom_id.id,
                        "product_uom_qty": 1.0,
                        "price_unit": 10.0,
                        "is_downpayment": True,
                    },
                ),
            ],
        }
        if values:
            vals.update(values)
        return self.env["sale.order"].create(vals)

    def test_update_lines_copies_list_price(self):
        order = self._create_sale_order()

        order.action_update_lines_from_list_price()

        line = order.order_line.filtered(lambda line: line.product_id == self.product)
        self.assertTrue(line)
        self.assertEqual(line.price_unit, self.product.list_price)

        # Downpayment and display lines remain unchanged
        down_line = order.order_line.filtered("is_downpayment")
        self.assertTrue(down_line)
        self.assertEqual(down_line.price_unit, 10.0)

    def test_invoiced_orders_are_skipped(self):
        order = self._create_sale_order()
        order.action_confirm()
        invoice = order._create_invoices()
        invoice.action_post()
        order._compute_invoice_status()
        self.assertEqual(order.invoice_status, "invoiced")

        # Change product price to ensure potential change
        order.order_line.filtered(
            lambda line: line.product_id == self.product
        ).price_unit = 5.0

        order.action_update_lines_from_list_price()

        # Price should remain untouched because order is invoiced
        line = order.order_line.filtered(lambda line: line.product_id == self.product)
        self.assertEqual(line.price_unit, 5.0)
