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

from odoo import api
from odoo import fields
from odoo import models


class ProductProduct(models.Model):
    _inherit = "product.product"

    origin_text = fields.Char(
        string="Origin",
        compute="_compute_origin_text",
        store=False,
        help="Origin text from main vendor's supplierinfo",
    )

    @api.depends("product_tmpl_id.main_seller_id", "seller_ids.origin_text")
    def _compute_origin_text(self):
        for product in self:
            if product.product_tmpl_id.main_seller_id:
                # Find the supplierinfo record for the main seller
                main_seller = product.product_tmpl_id.main_seller_id
                seller = product.seller_ids.filtered(
                    lambda s, ms=main_seller: s.partner_id == ms
                )[:1]
                product.origin_text = seller.origin_text if seller else False
            else:
                product.origin_text = False
