jQuery(document).ready(function ($) {
	"use strict";

	var deactivateUrl = "";

	// Close modal button
	$(document).on(
		"click",
		".stw-modal-cancel, .stw-modal-cancel-link",
		function (e) {
			e.preventDefault();
			$("#stw-deactivate-modal").removeClass("modal-active");
			resetFeedbackForm();
		},
	);

	// Close modal when clicking outside
	$(document).on("click", "#stw-deactivate-modal", function (event) {
		var $modalWrap = $(this).find(".stw-modal-wrap");
		if (
			!$modalWrap.is(event.target) &&
			$modalWrap.has(event.target).length === 0
		) {
			$(this).removeClass("modal-active");
			resetFeedbackForm();
		}
	});

	// Intercept deactivate link click - using exact ID from WordPress
	$(document).on("click", "#deactivate-3d-scan-and-show", function (e) {
		e.preventDefault();
		e.stopPropagation();
		deactivateUrl = $(this).attr("href");
		$("#stw-deactivate-modal").addClass("modal-active");
	});

	// Handle radio button selection
	$(document).on("change", 'input[name="stw-deactivate-reason"]', function () {
		var value = $(this).val();
		var $otherTextarea = $("#stw-deactivate-other-text");

		if (value === "other") {
			$otherTextarea.show().focus();
		} else {
			$otherTextarea.hide().val("");
		}

		// Enable submit button when a reason is selected
		$(".stw-modal-deactivate").prop("disabled", false);
	});

	// Reset feedback form
	const resetFeedbackForm = () => {
		$('input[name="stw-deactivate-reason"]').prop("checked", false);
		$("#stw-deactivate-other-text").hide().val("");
		$(".stw-modal-deactivate").prop("disabled", true);
		$(".stw-feedback-error").hide();
	};

	// Get feedback text based on selection
	const getFeedbackText = () => {
		var $selected = $('input[name="stw-deactivate-reason"]:checked');
		if (!$selected.length) {
			return null;
		}

		var value = $selected.val();
		if (value === "other") {
			return $("#stw-deactivate-other-text").val().trim() || "Other";
		}

		// Get the label text for the selected option
		return $selected.closest("label").find(".stw-reason-text").text().trim();
	};

	// Submit feedback and deactivate
	$(document).on("click", ".stw-modal-deactivate", function (e) {
		e.preventDefault();

		var $button = $(this);
		var feedbackText = getFeedbackText();

		if (!feedbackText) {
			$(".stw-feedback-error").text("Please select a reason").show();
			return;
		}

		// Disable button and show loading state
		$button.prop("disabled", true).text("Submitting...");
		$(".stw-feedback-error").hide();

		// Submit feedback to API
		$.ajax({
			url: stwDeactivation.restUrl + "feedback",
			method: "POST",
			headers: {
				"X-WP-Nonce": stwDeactivation.nonce,
			},
			contentType: "application/json",
			data: JSON.stringify({
				text: feedbackText,
			}),
			success: function () {
				// Proceed with deactivation
				window.location.href = deactivateUrl;
			},
			error: function () {
				// Even if feedback fails, proceed with deactivation
				window.location.href = deactivateUrl;
			},
		});
	});

	// Skip feedback and deactivate directly
	$(document).on("click", ".stw-modal-skip", function (e) {
		e.preventDefault();
		if (deactivateUrl) {
			window.location.href = deactivateUrl;
		}
	});
});
