/**
 * STRYKE WP — Plugin Meta Scripts
 *
 * Handles the deactivation feedback modal on plugins.php.
 * Injected logo CSS custom property for the plugin row.
 */
(function () {
	'use strict';

	if (typeof strykeWPMeta === 'undefined') {
		return;
	}

	var basename = strykeWPMeta.basename;
	var i18n     = strykeWPMeta.i18n;

	// =========================================================================
	// 1. Inject icon URL as CSS custom property
	// =========================================================================

	var pluginRow = document.querySelector('tr[data-plugin="' + basename + '"]');
	if (pluginRow) {
		var pluginDir = basename.split('/')[0];
		var iconUrl   = (typeof ajaxurl !== 'undefined' ? ajaxurl.replace('/wp-admin/admin-ajax.php', '') : '')
			+ '/wp-content/plugins/' + pluginDir + '/assets/img/icon-128x128.png';
		pluginRow.style.setProperty('--stryke-icon-url', 'url("' + iconUrl + '")');
	}

	// =========================================================================
	// 2. Deactivation Feedback Modal
	// =========================================================================

	var modal       = document.getElementById('stryke-wp-deactivation-modal');
	var deactivateLink = null;
	var originalHref   = '';

	if (!modal) {
		return;
	}

	// Populate modal text from localized strings.
	var titleEl   = document.getElementById('stryke-wp-modal-title');
	var messageEl = document.getElementById('stryke-wp-modal-message');
	var deleteEl  = document.getElementById('stryke-wp-modal-delete-label');
	var cancelBtn = document.getElementById('stryke-wp-modal-cancel');
	var confirmBtn = document.getElementById('stryke-wp-modal-confirm');

	if (titleEl) titleEl.textContent = i18n.title;
	if (messageEl) messageEl.textContent = i18n.confirm;
	if (deleteEl) deleteEl.textContent = i18n.deleteData;
	if (cancelBtn) cancelBtn.textContent = i18n.cancel;
	if (confirmBtn) confirmBtn.textContent = i18n.deactivate;

	// Find the deactivate link for our plugin.
	var deactivateLinks = document.querySelectorAll('tr[data-plugin="' + basename + '"] .deactivate a');
	if (deactivateLinks.length > 0) {
		deactivateLink = deactivateLinks[0];
		originalHref = deactivateLink.getAttribute('href');

		deactivateLink.addEventListener('click', function (e) {
			e.preventDefault();
			openModal();
		});
	}

	function openModal() {
		modal.style.display = '';
		document.getElementById('stryke-wp-delete-data').checked = false;
		document.body.style.overflow = 'hidden';
	}

	function closeModal() {
		modal.style.display = 'none';
		document.body.style.overflow = '';
	}

	// Close button.
	modal.querySelector('.stryke-wp-modal__close').addEventListener('click', closeModal);

	// Backdrop click.
	modal.querySelector('.stryke-wp-modal__backdrop').addEventListener('click', closeModal);

	// Cancel button.
	cancelBtn.addEventListener('click', closeModal);

	// Confirm button — proceed with deactivation.
	confirmBtn.addEventListener('click', function () {
		var deleteData = document.getElementById('stryke-wp-delete-data').checked;
		var href = originalHref;

		if (deleteData) {
			// Append delete_data flag. The uninstall.php will check for this option.
			// Store the preference as a WP option via a quick AJAX call.
			var xhr = new XMLHttpRequest();
			xhr.open('POST', ajaxurl, false); // Synchronous so we redirect after.
			xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xhr.send(
				'action=stryke_wp_set_delete_data&value=1&nonce=' +
				encodeURIComponent(strykeWPMeta.deleteDataNonce || '')
			);
		}

		closeModal();
		window.location.href = href;
	});

	// Escape key closes modal.
	document.addEventListener('keydown', function (e) {
		if (e.key === 'Escape' && modal.style.display !== 'none') {
			closeModal();
		}
	});
})();
