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

from odoo import fields
from odoo import models


class ResPartner(models.Model):
    """Extend res.partner with default price category for suppliers."""

    _inherit = "res.partner"

    default_price_category_id = fields.Many2one(
        comodel_name="product.price.category",
        string="Default Price Category",
        help="Default price category for products from this supplier",
        domain=[],
    )

    def action_update_products_price_category(self):
        """Open wizard to bulk update products with default price category."""
        self.ensure_one()

        # Count products where this partner is the default supplier
        product_count = self.env["product.template"].search_count(
            [("main_seller_id", "=", self.id)]
        )

        # Create wizard record with context data
        wizard = self.env["wizard.update.product.category"].create(
            {
                "partner_id": self.id,
                "partner_name": self.name,
                "price_category_id": self.default_price_category_id.id,
                "product_count": product_count,
            }
        )

        # Return action to open wizard modal
        return {
            "type": "ir.actions.act_window",
            "name": self.env._("Update Product Price Category"),
            "res_model": "wizard.update.product.category",
            "res_id": wizard.id,
            "view_mode": "form",
            "target": "new",
        }
