jQuery(document).ready(function (jQuery) {
  // console.log('RFA loaded!');
  // Initialize the media uploader when the button is clicked
  jQuery('#recaptcha-for-all-media-uploader-button').click(function (e) {
    //console.log('RFA click M U B');   
    var clsname = e.target.className;
    var idname = e.target.id;
    e.preventDefault();
    e.stopImmediatePropagation();
    //console.log('clicou...');
    var mediaUploader = wp.media({
      title: 'Select Image',
      multiple: false
    });
    // Handle image selection
    mediaUploader.on('select', function () {
      var attachment = mediaUploader.state().get('selection').first().toJSON();
      var imageUrl = attachment.url;
      //console.log(imageUrl);
      jQuery.ajax({
        url: ajaxurl,
        type: "POST",
        data: {
          action: 'recaptcha_for_all_image_select',
          recaptcha_image_url: imageUrl,
          recaptcha_my_plugin_nonce: recaptcha_my_data.recaptcha_my_nonce
        },
        success: function (data) {
          //console.log(data);
          jQuery('#recaptcha_for_all_custom_image_background').attr("src", imageUrl);
        },
        error: function (errorThrown) {
          console.log(errorThrown);
        }
      });
    });
    // Open the media uploader
    mediaUploader.open();
  });

  // CAPTCHA submission - COMPATÍVEL COM AMBOS (TURNSTILE E reCAPTCHA)
  jQuery("#recaptcha_for_all").click(function (e) {
    jQuery('#recaptcha_for_all').fadeOut(250).fadeIn(250);
    jQuery('#recaptcha_for_all').attr("disabled", true);
    e.preventDefault();
    e.stopImmediatePropagation();

    var $sitekey = jQuery("#sitekey").val();

    // DETECÇÃO AUTOMÁTICA - QUAL CAPTCHA ESTÁ ATIVO?
    if (typeof turnstile !== 'undefined') {
      // CLOUDFLARE TURNSTILE
      console.log('Usando Cloudflare Turnstile');
      turnstile.ready(function () {
        try {
          var widgetContainer = document.getElementById('turnstile-widget');
          if (!widgetContainer) {
            console.error('Container do Turnstile não encontrado');
            jQuery('#recaptcha_for_all').attr("disabled", false);
            return;
          }
          turnstile.execute(widgetContainer, {
            sitekey: $sitekey,
            action: 'recaptcha_for_all_main',
            callback: function (token) {
              jQuery('#recaptcha_for_all').prepend('<input type="hidden" name="cf-turnstile-response" value="' + token + '">');
              jQuery('#recaptcha_for_all').prepend('<input type="hidden" name="action" value="recaptcha_for_all">');
              jQuery("#recaptcha_for_all").submit();
            },
            'error-callback': function () {
              console.error('Erro no Turnstile');
              jQuery('#recaptcha_for_all').attr("disabled", false);
            }
          });
        } catch (error) {
          console.error('Erro na execução do Turnstile:', error);
          jQuery('#recaptcha_for_all').attr("disabled", false);
        }
      });
    }
    else if (typeof grecaptcha !== 'undefined') {
      // GOOGLE reCAPTCHA (código original mantido)
      console.log('Usando Google reCAPTCHA');
      grecaptcha.ready(function () {
        try {
          grecaptcha.execute($sitekey, { action: 'recaptcha_for_all_main' }).then(function (token) {
            jQuery('#recaptcha_for_all').prepend('<input type="hidden" name="token" value="' + token + '">');
            jQuery('#recaptcha_for_all').prepend('<input type="hidden" name="action" value="recaptcha_for_all">');
            jQuery("#recaptcha_for_all").submit();
          });
        } catch (error) {
          console.error('Error in reCAPTCHA execution:', error.message);
          jQuery('#recaptcha_for_all').attr("disabled", false);
        }
      });
    }
    else {
      console.log('Nenhuma biblioteca CAPTCHA carregada!');
      jQuery('#recaptcha_for_all').attr("disabled", false);
    }
  });
});