/**
 * ChargePanda SaaS Module — Pricing Model JS Handlers
 *
 * Registers the per_seat and metered pricing model handlers with the core
 * CP SDK. This script must load AFTER chargepanda-product.js and BEFORE
 * DOMContentLoaded fires (i.e. in the <body> footer, not deferred).
 *
 * These handlers mirror the PHP PricingModelContract implementations in
 * modules/Saas/PricingModels/.
 */
(function () {
    'use strict';

    if (typeof window.CP === 'undefined' || typeof window.CP.registerPricingModel !== 'function') {
        console.warn('ChargePanda SaaS: CP SDK not found. Pricing model handlers not registered.');
        return;
    }

    /**
     * Per-seat pricing model.
     * Base price (per_unit_amount) × number of seats selected by the customer.
     */
    CP.registerPricingModel('per_seat', {
        onActivate: function (elements) {
            if (elements.seatContainer) elements.seatContainer.classList.remove('hidden');
            if (elements.meteredNote)   elements.meteredNote.classList.add('hidden');
            if (elements.renewalText)   elements.renewalText.classList.remove('hidden');
        },
        calculateTotal: function (basePrice, state) {
            return basePrice * Math.max(1, state.seatCount);
        },
        getPayload: function (state) {
            return { seat_count: state.seatCount };
        }
    });

    /**
     * Metered (usage-based) pricing model.
     * $0 due at checkout; customer is billed post-period based on usage records.
     */
    CP.registerPricingModel('metered', {
        onActivate: function (elements) {
            if (elements.seatContainer) elements.seatContainer.classList.add('hidden');
            if (elements.meteredNote)   elements.meteredNote.classList.remove('hidden');
            if (elements.renewalText)   elements.renewalText.classList.add('hidden');
        },
        calculateTotal: function (basePrice, state) {
            return 0;
        },
        getPayload: function (state) {
            return {};
        }
    });

})();
