jQuery(document).ready(function ($) {
  "use strict";
  // Vérifier que les données sont disponibles
  if (typeof ncpcFreePromoData === "undefined") {
    console.warn("NCPC Free: Données de promotion manquantes");
    return;
  }

  // Configuration
  const config = {
    nonce: ncpcFreePromoData.nonce || "",
    ajaxUrl: ncpcFreePromoData.ajaxUrl || "",
    strings: ncpcFreePromoData.strings || {},
  };

  // Classe pour gérer la promotion
  class NcpcFreePromoHandler {
    constructor() {
      this.init();
    }

    init() {
      this.bindEvents();
    }

    bindEvents() {
      // Event pour "Découvrir"
      $(document).on("click", ".ncpc-free-discover-plugin", (e) => {
        this.handleDiscoverClick($(e.currentTarget));
      });

      // Events pour les boutons de dismiss
      $(document).on(
        "click",
        ".ncpc-free-dismiss-promo, .ncpc-free-dismiss-promo-link",
        (e) => {
          e.preventDefault();
          this.handleDismissPromo($(e.currentTarget));
        }
      );
    }

    handleDiscoverClick($element) {
      const pluginSlug = $element.data("plugin");

      // Animation de feedback
      this.animateIcon();

      // Tracker le clic (optionnel)
      this.trackPluginClick(pluginSlug);

      // Le lien s'ouvre automatiquement (target="_blank")
    }

    handleDismissPromo($element) {
      const dismissType = $element.data("action");
      const $widget = $("#ncpc-free-promo-widget");

      // Animation de sortie
      $widget.addClass("ncpc-free-promo-dismissing");

      setTimeout(() => {
        $widget.fadeOut(300, () => {
          $widget.remove();
        });
      }, 200);

      // Envoyer la requête
      this.sendDismissRequest(dismissType);
    }

    animateIcon() {
      const $icon = $(".ncpc-free-promo-icon");
      $icon.css({
        transform: "scale(1.3) rotate(10deg)",
        transition: "transform 0.3s ease",
      });

      setTimeout(() => {
        $icon.css("transform", "scale(1) rotate(0deg)");
      }, 300);
    }

    trackPluginClick(pluginSlug) {
      // Tracker les clics pour analytics
      $.ajax({
        url: config.ajaxUrl,
        type: "POST",
        data: {
          action: "ncpc_free_track_plugin_click",
          plugin_slug: pluginSlug,
          nonce: config.nonce,
        },
        success: (response) => {
          console.log("NCPC Free: Click tracked for", pluginSlug);
        },
      });
    }

    sendDismissRequest(dismissType) {
      if (!config.nonce || !config.ajaxUrl) {
        console.warn("NCPC Free: Configuration manquante pour la requête AJAX");
        return;
      }

      $.ajax({
        url: config.ajaxUrl,
        type: "POST",
        data: {
          action: "ncpc_free_dismiss_promo",
          dismiss_type: dismissType,
          nonce: config.nonce,
        },
        success: (response) => {
          if (response.success) {
            console.log("NCPC Free Promo:", response.data);
          }
        },
        error: (xhr, status, error) => {
          console.error("NCPC Free: Erreur lors du dismiss promo:", error);
        },
      });
    }
  }

  $("<style>")
    .text(
      `
        .ncpc-free-promo-dismissing {
            transform: translateX(-100%);
            opacity: 0;
            transition: all 0.3s ease-out !important;
        }
    `
    )
    .appendTo("head");

  new NcpcFreePromoHandler();

  $(".ncpc-pronc-dismiss, .ncpc-pronc-dismiss-link").on("click", function (e) {
    e.preventDefault();

    const action = $(this).data("action");
    const $widget = $("#ncpc-pronc-notice-widget");

    // Animation de fermeture
    $widget.fadeOut(300);

    // Envoi AJAX
    $.ajax({
      url: config.ajaxUrl,
      type: "POST",
      data: {
        action: "ncpc_dismiss_pro_notice",
        dismiss_action: action,
        nonce: config.nonce,
      },
      success: function (response) {
        console.log("Notice closed:", action, "response: ", response);
      },
    });
  });
});
