# Copyright (C) 2020: Criptomart (https://criptomart.net)
# @author Santi Noreña (<santi@criptomart.net>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging

from odoo import models

_logger = logging.getLogger(__name__)


class ProductPricelist(models.Model):
    _inherit = "product.pricelist"

    def _compute_price_rule(self, products, quantity, uom=None, date=False, **kwargs):
        ProductPricelistItem = self.env["product.pricelist.item"]
        ProductProduct = self.env["product.product"]

        _logger.info(
            "[PRICELIST DEBUG] _compute_price_rule called with products=%s (model=%s), quantity=%s",
            products.ids,
            products._name,
            quantity,
        )

        res = super()._compute_price_rule(
            products, quantity, uom=uom, date=date, **kwargs
        )

        _logger.info(
            "[PRICELIST DEBUG] super()._compute_price_rule returned: %s",
            res,
        )

        new_res = res.copy()
        item_id = []
        for product_id, values in res.items():
            if values[1]:
                item_id = values[1]
            if item_id:
                item = ProductPricelistItem.browse(item_id)
                _logger.info(
                    "[PRICELIST DEBUG] Product %s: item.base=%s, item_id=%s",
                    product_id,
                    item.base,
                    item_id,
                )
                if item.base == "last_purchase_price":
                    # product_id could be from product.template or product.product
                    # Check which model we're working with
                    if products._name == "product.template":
                        # Get the variant from the template
                        template = products.browse(product_id)
                        if template.exists():
                            product = template.product_variant_id
                        else:
                            _logger.warning(
                                "[PRICELIST] Template ID %s not found in products",
                                product_id,
                            )
                            continue
                    else:
                        product = ProductProduct.browse(product_id)

                    price = product.last_purchase_price_received
                    _logger.info(
                        "[PRICELIST DEBUG] Product %s: last_purchase_price_received=%s, item.price_discount=%s",
                        product_id,
                        price,
                        item.price_discount,
                    )
                    price = (price - (price * (item.price_discount / 100))) or 0.0
                    new_res[product_id] = (price, item_id)
                    _logger.info(
                        "[PRICELIST DEBUG] Product %s: calculated price=%s",
                        product_id,
                        price,
                    )
        return new_res
