/**
 * Welcome Notice JavaScript
 *
 * Handles User Registration & Membership installation and activation from the welcome notice.
 *
 * @package AllCoursesFSE
 */

(function ($) {
	"use strict";

	const welcomeNotice = {
		/**
		 * Initialize the welcome notice.
		 */
		init: function () {
			this.params = window.member_read_welcome_notice_params || {};
			this.bindEvents();
		},

		/**
		 * Bind events.
		 */
		bindEvents: function () {
			const self = this;

			// Handle dismiss button.
			$(".member-read-welcome-notice .notice-dismiss").on(
				"click",
				function (e) {
					e.preventDefault();
					self.dismissNotice();
				},
			);

			// Handle install/activate button.
			$("#member-read-install-urm").on("click", function (e) {
				e.preventDefault();
				self.handleURMInstall($(this));
			});
		},

		/**
		 * Dismiss the welcome notice.
		 */
		dismissNotice: function () {
			const self = this;

			$.ajax({
				url: self.params.ajaxUrl,
				type: "POST",
				data: {
					action: "member_read_dismiss_welcome_notice",
					nonce: self.params.nonce,
				},
				success: function () {
					$(".member-read-welcome-notice").fadeOut();
				},
			});
		},

		/**
		 * Handle URM installation/activation.
		 *
		 * @param {jQuery} $button The button element.
		 */
		handleURMInstall: function ($button) {
			const self = this;
			const status = self.params.urmStatus;

			// Disable button and show loading state.
			$button.prop("disabled", true);
			$button.find(".dashicons").removeClass("hidden");

			if (status === "installed") {
				// Just activate the plugin.
				self.activateURM($button);
			} else {
				// Install and then activate.
				self.installURM($button);
			}
		},

		/**
		 * Install User Registration & Membership.
		 *
		 * @param {jQuery} $button The button element.
		 */
		installURM: function ($button) {
			const self = this;

			$button.find(".text").text(self.params.installing);

			wp.updates.ajax("install-plugin", {
				slug: "user-registration",
				success: function () {
					// Plugin installed, now activate it.
					self.activateURM($button);
				},
				error: function (error) {
					console.error("Installation failed:", error);
					$button.find(".dashicons").addClass("hidden");
					$button.prop("disabled", false);
					alert(
						"Failed to install User Registration & Membership. Please try again or install manually.",
					);
				},
			});
		},

		/**
		 * Activate User Registration & Membership.
		 *
		 * @param {jQuery} $button The button element.
		 */
		activateURM: function ($button) {
			const self = this;

			$button.find(".text").text(self.params.activating);

			// Set the reference before activation.
			self.setURMReference(function () {
				// Redirect to activation URL.
				window.location.href = self.params.activationUrl;
			});
		},

		/**
		 * Set URM reference key.
		 *
		 * @param {Function} callback Callback function after setting reference.
		 */
		setURMReference: function (callback) {
			const self = this;

			$.ajax({
				url: self.params.ajaxUrl,
				type: "POST",
				data: {
					action: "member_read_set_urm_ref",
					nonce: self.params.urmRefNonce,
				},
				success: function () {
					if (callback && typeof callback === "function") {
						callback();
					}
				},
				error: function () {
					// Continue even if reference setting fails.
					if (callback && typeof callback === "function") {
						callback();
					}
				},
			});
		},
	};

	// Initialize on document ready.
	$(document).ready(function () {
		welcomeNotice.init();
	});
})(jQuery);
