jQuery(function ($) {
    'use strict';

    const StoreCreditHandler = {
        init: function () {
            // Use the correct IDs from the template
            $(document.body).on('click', '#unlockafe-apply-store-credit', this.applyStoreCredit);
            $(document.body).on('click', '#unlockafe-remove-store-credit', this.removeStoreCredit);
        },

        block: function () {
            const $container = $('.unlockafe-store-credit-container').closest('.cart-collaterals, form.woocommerce-checkout');
            if ($container.length) {
                $container.block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } });
            } else {
                 $('.woocommerce-cart-form, .woocommerce-checkout').block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } });
            }
        },

        unblock: function () {
            const $container = $('.unlockafe-store-credit-container').closest('.cart-collaterals, form.woocommerce-checkout');
             if ($container.length) {
                $container.unblock();
            } else {
                $('.woocommerce-cart-form, .woocommerce-checkout').unblock();
            }
        },

        showNotice: function (message, type = 'success') {
            const $notice = $('.unlockafe-store-credit-notice');
            $notice.html(`<div class="woocommerce-message">${message}</div>`);
            if (type === 'error') {
                $notice.find('> div').removeClass('woocommerce-message').addClass('woocommerce-error');
            }
            $notice.slideDown();
            setTimeout(() => {
                $notice.slideUp(() => $notice.empty());
            }, 5000);
        },

        applyStoreCredit: function (e) {
            e.preventDefault();
            StoreCreditHandler.block();

            $.ajax({
                type: 'POST',
                url: unlockafe_store_credit.ajax_url,
                data: {
                    action: 'unlockafe_apply_store_credit',
                    nonce: unlockafe_store_credit.apply_nonce // Use localized nonce
                },
                success: function (response) {
                    if (response.success) {
                        StoreCreditHandler.showNotice(response.data.message);
                        $(document.body).trigger('update_checkout');
                        $(document.body).trigger('wc_update_cart');
                    } else {
                        StoreCreditHandler.showNotice(response.data.message, 'error');
                    }
                    StoreCreditHandler.unblock();
                },
                error: function () {
                    StoreCreditHandler.unblock();
                    StoreCreditHandler.showNotice('An unknown error occurred.', 'error');
                }
            });
        },

        removeStoreCredit: function (e) {
            e.preventDefault();
            StoreCreditHandler.block();

            $.ajax({
                type: 'POST',
                url: unlockafe_store_credit.ajax_url,
                data: {
                    action: 'unlockafe_remove_store_credit',
                    nonce: unlockafe_store_credit.remove_nonce // Use localized nonce
                },
                success: function (response) {
                    if (response.success) {
                        StoreCreditHandler.showNotice(response.data.message);
                        $(document.body).trigger('update_checkout');
                        $(document.body).trigger('wc_update_cart');
                    } else {
                        StoreCreditHandler.showNotice(response.data.message, 'error');
                    }
                    StoreCreditHandler.unblock();
                },
                error: function () {
                    StoreCreditHandler.unblock();
                    StoreCreditHandler.showNotice('An unknown error occurred.', 'error');
                }
            });
        }
    };

    StoreCreditHandler.init();
});
