// Product Pricing JavaScript - Handles multi-plan selection and feature toggles
document.addEventListener('DOMContentLoaded', function () {
    // Plan selection handler
    const planRadios = document.querySelectorAll('.plan-radio');
    const totalDueElement = document.getElementById('total-due-price');
    const pricingTierInput = document.getElementById('selected-pricing-tier-id');

    if (planRadios.length > 0) {
        planRadios.forEach(radio => {
            radio.addEventListener('change', function () {
                updateTotalPrice();

                // Update hidden pricing tier input
                if (pricingTierInput) {
                    pricingTierInput.value = this.getAttribute('data-tier-id') || '';
                }
            });
        });
    }

    // Add-on checkbox handler
    const addonCheckboxes = document.querySelectorAll('.addon-checkbox');
    addonCheckboxes.forEach(checkbox => {
        checkbox.addEventListener('change', updateTotalPrice);
    });

    // Feature toggle handler
    const featureToggles = document.querySelectorAll('.toggle-features-btn');
    featureToggles.forEach(btn => {
        btn.addEventListener('click', function (e) {
            e.preventDefault();
            const card = this.closest('.plan-card');
            const extraFeatures = card.querySelector('.extra-features');
            const icon = this.querySelector('.material-symbols-outlined');
            const text = this.querySelector('span:first-child');

            if (extraFeatures) {
                const isHidden = extraFeatures.classList.contains('hidden');

                if (isHidden) {
                    extraFeatures.classList.remove('hidden');
                    icon.style.transform = 'rotate(180deg)';
                    text.textContent = 'Show less';
                } else {
                    extraFeatures.classList.add('hidden');
                    icon.style.transform = 'rotate(0deg)';
                    text.textContent = 'Show all features';
                }
            }
        });
    });

    function updateTotalPrice() {
        if (!totalDueElement) return;

        // Get selected plan price
        const selectedPlan = document.querySelector('.plan-radio:checked');
        let total = 0;

        if (selectedPlan) {
            const planPrice = parseFloat(selectedPlan.getAttribute('data-price') || 0);
            total += planPrice;
        }

        // Add checked add-ons
        const checkedAddons = document.querySelectorAll('.addon-checkbox:checked');
        checkedAddons.forEach(addon => {
            const addonPrice = parseFloat(addon.getAttribute('data-price') || 0);
            total += addonPrice;
        });

        // Update total display
        totalDueElement.textContent = '$' + total.toFixed(2);
    }
});
