/**
 * AI Marketing Notice - Admin JavaScript
 *
 * @package Ai_Blog
 */

/* global jQuery, aiBlogMarketingNotice */
(function ($) {
	'use strict';

	var $notice = $('#ai-blog-marketing-notice');
	var $installBtn = $('#ai-blog-install-plugin');
	var $dismissBtn = $('#ai-blog-dismiss-notice');
	var $closeBtn = $('#ai-blog-notice-close');

	/**
	 * Dismiss notice with animation.
	 */
	function dismissNotice() {
		$notice.addClass('ai-blog-notice-exit');
		setTimeout(function () {
			$notice.remove();
		}, 350);
	}

	// ------------------------------------------
	// Install & Activate Plugin
	// ------------------------------------------
	$installBtn.on('click', function (e) {
		e.preventDefault();

		if ($installBtn.hasClass('loading')) {
			return;
		}

		$installBtn.addClass('loading');

		$.ajax({
			url: aiBlogMarketingNotice.ajaxurl,
			type: 'POST',
			data: {
				action: 'ai_blog_install_marketing_plugin',
				nonce: aiBlogMarketingNotice.nonce,
				slug: $installBtn.data('slug'),
				action_type: $installBtn.data('action'),
			},
			success: function (response) {
				if (response.success) {
					$installBtn.removeClass('loading').addClass('success');
					$installBtn.find('.ai-blog-btn-label').text(response.data.message);
					setTimeout(function () {
						window.location.reload();
					}, 1200);
				} else {
					$installBtn.removeClass('loading');
					alert(response.data.message);
				}
			},
			error: function () {
				$installBtn.removeClass('loading');
				alert(aiBlogMarketingNotice.error);
			},
		});
	});

	// ------------------------------------------
	// Close button (X) — temporary hide only
	// ------------------------------------------
	$closeBtn.on('click', function (e) {
		e.preventDefault();
		dismissNotice();
	});

	// ------------------------------------------
	// "Maybe Later" — permanent dismiss
	// ------------------------------------------
	$dismissBtn.on('click', function (e) {
		e.preventDefault();
		dismissNotice();

		$.ajax({
			url: aiBlogMarketingNotice.ajaxurl,
			type: 'POST',
			data: {
				action: 'ai_blog_dismiss_marketing_notice',
				nonce: aiBlogMarketingNotice.nonce,
			},
		});
	});

	// ------------------------------------------
	// Stagger entrance for highlight items & modules
	// ------------------------------------------
	$(document).ready(function () {
		// Highlight items
		$('.ai-blog-highlight-item').each(function (i) {
			var $el = $(this);
			$el.css({ opacity: 0, transform: 'scale(0.9)' });
			setTimeout(function () {
				$el.css({
					transition: 'opacity 0.4s ease, transform 0.4s ease',
					opacity: 1,
					transform: 'scale(1)',
				});
			}, 200 + i * 150);
		});

		// Module pills
		$('.ai-blog-module').each(function (i) {
			var $el = $(this);
			$el.css({ opacity: 0, transform: 'translateY(6px)' });
			setTimeout(function () {
				$el.css({
					transition: 'opacity 0.3s ease, transform 0.3s ease',
					opacity: 1,
					transform: 'translateY(0)',
				});
			}, 400 + i * 80);
		});
	});
})(jQuery);
