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

from odoo import fields, models


class Website(models.Model):
    _inherit = "website"

    membership_signup_required = fields.Boolean(
        string="Force signup for membership products",
        help="When enabled, anonymous users trying to add a membership product "
             "to cart are redirected to /web/signup before the product is "
             "added. Only affects the e-commerce flow.",
        default=True,
    )

    def _membership_signup_required_for(self, product_id, is_public):
        """Decide whether the given `product_id` requires forcing a signup
        step before it can be added to cart by an anonymous visitor.

        Centralised here (model layer) so it can be unit-tested without HTTP
        and reused by future extensions. The controller delegates to this
        helper.
        """
        self.ensure_one()
        if not self.membership_signup_required:
            return False
        if not is_public:
            return False
        if not product_id:
            return False
        product = self.env["product.product"].sudo().browse(int(product_id)).exists()
        return bool(product and product.product_tmpl_id.membership)