# 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 WizardUpdateProductCategory(models.TransientModel):
    """Wizard to confirm and bulk update product price categories."""

    _name = "wizard.update.product.category"
    _description = "Update Product Price Category"

    partner_id = fields.Many2one(
        comodel_name="res.partner",
        string="Supplier",
        readonly=True,
        required=True,
    )

    partner_name = fields.Char(
        string="Supplier Name",
        readonly=True,
        related="partner_id.name",
    )

    price_category_id = fields.Many2one(
        comodel_name="product.price.category",
        string="Price Category",
        readonly=True,
        required=True,
    )

    product_count = fields.Integer(
        string="Number of Products",
        readonly=True,
        required=True,
    )

    def action_confirm(self):
        """Bulk update all products from supplier with default price category."""
        self.ensure_one()

        # Search all products where this partner is the default supplier
        products = self.env["product.template"].search(
            [("main_seller_id", "=", self.partner_id.id)]
        )

        if not products:
            return {
                "type": "ir.actions.client",
                "tag": "display_notification",
                "params": {
                    "title": self.env._("No Products"),
                    "message": self.env._("No products found with this supplier."),
                    "type": "warning",
                    "sticky": False,
                },
            }

        # Bulk update all products
        products.write({"price_category_id": self.price_category_id.id})

        return {
            "type": "ir.actions.client",
            "tag": "display_notification",
            "params": {
                "title": self.env._("Success"),
                "message": self.env._(
                    "%(count)d products updated with category %(category)s",
                    count=len(products),
                    category=self.price_category_id.display_name,
                ),
                "type": "success",
                "sticky": False,
            },
        }
