jQuery(document).ready(function($) {
    // Check if we have the required data
    if (typeof formshieldDiviData === 'undefined') {
        console.warn('FormShield: Divi badge data not found');
        return;
    }

    if (!formshieldDiviData.badge || !formshieldDiviData.protectedForms) {
        console.warn('FormShield: Missing required badge data', formshieldDiviData);
        return;
    }

    // Track which forms already have badges to prevent duplicates
    var badgeAdded = false;

    // Function to add badge to Divi forms
    function addDiviBadge() {
        // If badge already added, don't add again
        if (badgeAdded) {
            return;
        }

        // Check if badge already exists on page
        if ($('.formshield-protected-badge, .formshield-divi-badge').length > 0) {
            badgeAdded = true;
            return;
        }

        // Find the main Divi contact form container (not individual fields)
        // Look for the outermost container with et_pb_contact_form class
        var $mainContainer = null;
        
        // First, try to find the main Divi contact form container
        $('.et_pb_contact_form').each(function() {
            var $container = $(this);
            // Check if this is a top-level container (not nested inside another et_pb_contact_form)
            if ($container.closest('.et_pb_contact_form').length === 0 || 
                $container.closest('.et_pb_contact_form')[0] === $container[0]) {
                $mainContainer = $container;
                return false; // Break loop
            }
        });

        // If not found, try finding form element inside Divi containers
        if (!$mainContainer || $mainContainer.length === 0) {
            $('form').each(function() {
                var $form = $(this);
                var $parent = $form.closest('[class*="et_pb_contact"], [class*="et_pb_module_contact"]');
                if ($parent.length > 0) {
                    // Check if this parent is not nested inside another contact form
                    if ($parent.closest('[class*="et_pb_contact"]').length === 0 || 
                        $parent.closest('[class*="et_pb_contact"]')[0] === $parent[0]) {
                        $mainContainer = $parent;
                        return false; // Break loop
                    }
                }
            });
        }

        // If still not found, try finding by form element with Divi attributes
        if (!$mainContainer || $mainContainer.length === 0) {
            $('form').each(function() {
                var $form = $(this);
                if ($form.hasClass('et_pb_contact_form') || 
                    $form.attr('data-et_pb_module_class') === 'et_pb_contact_form' ||
                    $form.find('[class*="et_pb_contact"]').length > 0) {
                    $mainContainer = $form.closest('[class*="et_pb_contact"]') || $form.parent();
                    return false; // Break loop
                }
            });
        }

        // Add badge only once after the main container
        if ($mainContainer && $mainContainer.length > 0) {
            var badgeHtml = formshieldDiviData.badge;
            if (badgeHtml) {
                // Check if badge already exists near this container
                var $existingBadge = $mainContainer.siblings('.formshield-protected-badge, .formshield-divi-badge').first();
                if ($existingBadge.length === 0) {
                    // Add badge after the main container
                    $mainContainer.after(badgeHtml);
                    badgeAdded = true;
                }
            }
        }
    }
    
    // Add badge when DOM is ready
    addDiviBadge();
    
    // Add badge after a short delay (for dynamically loaded forms) - only if not already added
    setTimeout(function() {
        if (!badgeAdded) {
            addDiviBadge();
        }
    }, 500);
    
    // Add badge when Divi forms are loaded via AJAX or other dynamic methods
    $(document).on('DOMNodeInserted', function(e) {
        if (!badgeAdded && $(e.target).find('.et_pb_contact_form, [class*="et_pb_contact_form"]').length > 0) {
            setTimeout(function() {
                if (!badgeAdded) {
                    addDiviBadge();
                }
            }, 100);
        }
    });
});
