jQuery(document).ready(function($) {
    // Check if we have the required data
    if (typeof formshieldCF7Data === "undefined") {
        return;
    }
    if (!formshieldCF7Data.protectedForms || !formshieldCF7Data.badge) {
        return;
    }

    // Function to add badge to CF7 forms
    function addCF7Badge() {
        // Find all CF7 forms
        var $forms = $(".wpcf7-form");
        
        $forms.each(function() {
            var $form = $(this);
            var $container = $form.closest(".wpcf7");
            
            if (!$container.length) {
                return;
            }
            
            var formId = $container.attr("id") || "";
            
            // Extract form ID from container ID (e.g., wpcf7-f123-p4-o1 -> 123)
            var matches = formId.match(/wpcf7-f(\d+)/);
            if (!matches) {
                return;
            }
            
            var cf7Id = "cf7-" + matches[1];
            
            // Check if this form is protected
            var isProtected = formshieldCF7Data.protectedForms.indexOf(cf7Id) !== -1;
            
            // Only add badge if form is protected and doesn't already have one
            if (isProtected) {
                var $existingBadge = $container.find(".formshield-protected-badge");
                if ($existingBadge.length === 0) {
                    $container.append(formshieldCF7Data.badge);
                }
            }
        });
    }
    
    // Add badge when DOM is ready
    addCF7Badge();
    
    // Add badge when forms are loaded via AJAX
    $(document).on("wpcf7mailsent wpcf7reset wpcf7invalid wpcf7valid wpcf7submit wpcf7spam wpcf7mailfailed", function(e) {
        setTimeout(addCF7Badge, 100);
    });
    
    // Also try to add badge after a short delay in case forms load later
    setTimeout(function() {
        addCF7Badge();
    }, 1000);
    
    // Try again after CF7 is fully loaded
    $(document).on('wpcf7:init', function() {
        addCF7Badge();
    });
    
    // Also try when CF7 form is found
    $(document).on('DOMNodeInserted', '.wpcf7', function() {
        addCF7Badge();
    });
});

// Also try to run immediately in case jQuery is not ready
if (typeof jQuery !== "undefined") {
    jQuery(function($) {
        if (typeof formshieldCF7Data !== "undefined" && formshieldCF7Data.protectedForms && formshieldCF7Data.badge) {
            var $forms = $(".wpcf7-form");
            $forms.each(function() {
                var $form = $(this);
                var $container = $form.closest(".wpcf7");
                if ($container.length) {
                    var formId = $container.attr("id") || "";
                    var matches = formId.match(/wpcf7-f(\d+)/);
                    if (matches) {
                        var cf7Id = "cf7-" + matches[1];
                        var isProtected = formshieldCF7Data.protectedForms.indexOf(cf7Id) !== -1;
                        if (isProtected) {
                            var $existingBadge = $container.find(".formshield-protected-badge");
                            if ($existingBadge.length === 0) {
                                $container.append(formshieldCF7Data.badge);
                            }
                        }
                    }
                }
            });
        }
    });
}
