// FormShield Protection Script
jQuery(document).ready(function($) {

    const FormShieldProtection = {

        init: function() {
            if ($('form').length) {
                this.injectHoneypots();
                this.setupFormValidation();
            }
        },

        isWooCommerceForm: function(form) {
            const $form = $(form);
            if ($form.hasClass('cart') || $form.hasClass('woocommerce-cart-form') ||
                $form.closest('.woocommerce').length > 0 ||
                $form.closest('.product').length > 0) {
                return true;
            }
            if ($form.hasClass('checkout') || $form.hasClass('woocommerce-checkout') ||
                $form.attr('name') === 'checkout' || $form.attr('id') === 'checkout') {
                return true;
            }
            if ($form.find('input[name="add-to-cart"]').length > 0 ||
                $form.find('button[name="add-to-cart"]').length > 0) {
                return true;
            }
            if ($form.hasClass('variations_form') || $form.find('.variations').length > 0) {
                return true;
            }
            if ($form.hasClass('woocommerce-form') ||
                $form.hasClass('woocommerce-form-login') ||
                $form.hasClass('woocommerce-form-register')) {
                return true;
            }
            return false;
        },

        injectHoneypots: function() {
            $('form').each((i, form) => {
                if (this.isWooCommerceForm(form)) {
                    return;
                }
                const honeypotId = 'fs_hp_' + Math.random().toString(36).substr(2, 9);
                $('<input>', {
                    type: 'text',
                    name: honeypotId,
                    id: honeypotId,
                    autocomplete: 'off'
                }).css({
                    position: 'absolute',
                    left: '-9999px',
                    opacity: 0,
                    height: 0,
                    width: 0
                }).appendTo(form);
            });
        },

        setupFormValidation: function() {
            $('form').on('submit', (e) => {
                const form = e.target;
                if (this.isWooCommerceForm(form)) {
                    return true;
                }
                // Client-side: only honeypot check.
                // Content-based spam detection runs server-side.
                const honeypot = $(form).find('input[name^="fs_hp_"]');
                if (honeypot.length && honeypot.val()) {
                    e.preventDefault();
                    e.stopPropagation();
                    this.showError(form);
                    return false;
                }
            });
        },

        showError: function(form) {
            const errorDiv = $('<div>', {
                class: 'formshield-error',
                text: 'Sorry, we could not process your submission. Please try again.'
            }).css({
                color: '#dc3545',
                padding: '10px',
                marginBottom: '10px',
                border: '1px solid #dc3545',
                borderRadius: '4px',
                backgroundColor: '#fff'
            });
            $(form).prepend(errorDiv);
            $('html, body').animate({ scrollTop: errorDiv.offset().top - 100 }, 500);
            setTimeout(() => errorDiv.fadeOut('slow', function() { $(this).remove(); }), 5000);
        }
    };

    FormShieldProtection.init();
});
