<?php // phpcs:ignore
/**
 * Initialization Action.
 *
 * @package ULTP // CHANGE THIS.
 */
namespace ULTP\Includes\Notice; // CHANGE THIS.

defined( 'ABSPATH' ) || exit;

/**
 * Initialization class.
 */
class WowRevenuePromotion {

	private const VERSION              = '20'; // Cache buster.
	private const PRODUCTS_MENU_SLUG   = 'revx-promo-wc-menu';
	private const PROMOTED_PLUGIN_SLUG = 'revenue';
	private const PROMOTED_PLUGIN_FILE = 'revenue/revenue.php';

	/**
	 * Setup class.
	 */
	public function __construct() {
		add_filter( 'revx_promo_promotion_hooks', array( $this, 'load' ) );
		add_action( 'plugins_loaded', array( $this, 'run_promotions' ) );
	}

	/**
	 * Run promotions.
	 *
	 * @return void
	 */
	public function run_promotions() {
		$is_active = ultimate_post()->is_lc_active();
		if ( $is_active || ! class_exists( '\WooCommerce' ) ||
			defined( 'REVENUE_VER' )
		) {
			return;
		}

		if ( $GLOBALS['revx_promo_promotion']['init'] ?? false ) {
			return;
		}

		$GLOBALS['revx_promo_promotion'] = array(
			'init' => true,
		);

		$hooks = apply_filters( 'revx_promo_promotion_hooks', array() );

		if ( ! is_array( $hooks ) ) {
			return;
		}

		uksort( $hooks, 'version_compare' );

		$latest_hook = end( $hooks );

		if ( is_callable( $latest_hook ) ) {
			$latest_hook();
		}
	}

	/**
	 * Load plugin
	 *
	 * @param array $callbacks Callbacks.
	 * @return array
	 */
	public function load( $callbacks ) {

		if ( isset( $callbacks[ self::VERSION ] ) ) {
			return $callbacks;
		}

		$callbacks[ self::VERSION ] = function () {
			// Dismiss actions.
			add_action( 'wp_ajax_revx_promo_dismiss_promotion', array( $this, 'ajax_dismiss_promotion' ) );
			add_action( 'wp_ajax_revx_promo_install_promotion_plugin', array( $this, 'ajax_install_promotion_plugin' ) );

			// Promotions.
			// ------------------.

			// Add Options Tab.
			add_filter( 'woocommerce_product_data_tabs', array( $this, 'register_product_bundle_tab' ) );
			add_action( 'woocommerce_product_data_panels', array( $this, 'render_product_bundle_panel' ) );

			// Product category page.
			add_action( 'product_cat_add_form_fields', array( $this, 'render_product_category_add_notice' ) );

			// Linked product section notice.
			add_action( 'woocommerce_product_options_related', array( $this, 'render_linked_products_notice' ) );

			// WooCommerce settings header notice.
			add_action( 'admin_notices', array( $this, 'render_settings_header_notice' ) );

			// Marketing > Coupons notice.
			add_action( 'admin_notices', array( $this, 'render_coupons_page_notice' ) );

			// Products submenu modal.
			add_action( 'admin_menu', array( $this, 'add_products_submenu' ), 9999 );
			add_action( 'admin_footer', array( $this, 'render_products_submenu_modal' ) );
		};

		return $callbacks;
	}

	/**
	 * Render promotion notice in the product category create form.
	 *
	 * @return void
	 */
	public function render_product_category_add_notice() {
		if ( ! $this->should_show_promotion( 'product_category_options' ) ) {
			return;
		}

		$this->render_promotion_notice(
			'revx-product-category',
			'product_category_options',
			'Turn Categories into Bundle Offers',
			'display:none;',
			true,
			array(
				'default' => 'Start Now',
				'loading' => 'Starting...',
			),
		);

		ob_start();
		?>
		<script>
			jQuery( function( $ ) {
				$(document).ready(function() {
					$( '#revx-product-category' ).insertAfter( '#addtag').slideDown(300);
				});
			} );
		</script>
		<?php
		echo ob_get_clean(); // phpcs:ignore
	}

	/**
	 * Add the promotional submenu item under Products.
	 *
	 * @return void
	 */
	public function add_products_submenu() {
		add_submenu_page(
			'woocommerce',
			'Create Bundle Offers',
			'Create Bundle Offers',
			'edit_posts',
			self::PRODUCTS_MENU_SLUG,
			'__return_null',
			6
		);

		add_submenu_page(
			'edit.php?post_type=product',
			'Create Bundle Offers',
			'Create Bundle Offers',
			'edit_posts',
			self::PRODUCTS_MENU_SLUG,
			'__return_null',
			6
		);
	}

	/**
	 * Register a promotion tab in the WooCommerce product data metabox.
	 *
	 * @param array $tabs Existing product data tabs.
	 * @return array
	 */
	public function register_product_bundle_tab( $tabs ) {
		if ( ! is_array( $tabs ) || ! $this->should_show_promotion( 'upsell_and_bundle' ) ) {
			return $tabs;
		}

		$tabs['revx-promo-upsell-and-bundle'] = array(
			'label'    => 'Upsell & Bundle',
			'target'   => 'revx-promo-add-product-options',
			'class'    => array( 'hide_if_grouped' ),
			'priority' => 10,
		);

		return $tabs;
	}

	/**
	 * Render the promotion panel for the product options tab.
	 *
	 * @return void
	 */
	public function render_product_bundle_panel() {
		if ( ! $this->should_show_promotion( 'upsell_and_bundle' ) ) {
			return;
		}

		?>
		<div id="revx-promo-add-product-options" class="panel woocommerce_options_panel">
			<?php
			$this->render_promotion_notice(
				'revx-promo-add-product-options',
				'upsell_and_bundle',
				'
					<div style="font-weight:bold;margin-bottom:12px;font-size:large;">
						Create High-Converting Bundle Offers in Minutes
					</div>
					<div style="margin-bottom:12px;">
						Bundle products, offer discounts, and increase your average order value - no setup hassle.
					</div>
					<ul style="padding-left: 1.5rem;">
						<li>
							<span style="font-weight:bold;">
								&#10003;&nbsp;
							</span>
							Increase order value with smart, strategic bundles
						</li>
						<li>
							<span style="font-weight:bold;">
								&#10003; &nbsp;
							</span>
							Turn browsers into buyers with irresistible discounts
						</li>
						<li>
							<span style="font-weight:bold;">
								&#10003; &nbsp;
							</span>
							Seamlessly integrates with your WooCommerce store
						</li>
					</ul>
				',
				'margin-inline: 20px;background:#f2f2f2;',
				true,
				array(
					'default' => 'Start Now',
					'loading' => 'Starting...',
				),
				'align-items:start;flex-direction:column;'
			);
			?>
		</div>
		<?php
	}

	/**
	 * Render promotion notice in the linked products section.
	 *
	 * @return void
	 */
	public function render_linked_products_notice() {
		if ( ! $this->should_show_promotion( 'linked_products_section' ) ) {
			return;
		}

		$this->render_promotion_notice(
			'revx-promo-linked-products',
			'linked_products_section',
			'Create Smart Bundles & Upsells',
			'margin:12px;width:fit-content;padding-right:50px;background:#f2f2f2;',
			true,
			array(
				'default' => 'Start Now',
				'loading' => 'Starting...',
			)
		);
	}

	/**
	 * Render a header notice on the WooCommerce settings page.
	 *
	 * @return void
	 */
	public function render_settings_header_notice() {
		global $pagenow;

		$page = sanitize_text_field( wp_unslash( $_GET['page'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

		if (
			! $this->should_show_promotion( 'settings_header' ) ||
			'admin.php' !== $pagenow ||
			'wc-settings' !== $page
		) {
			return;
		}

		$this->render_promotion_notice(
			'revx-promo-settings-header',
			'settings_header',
			'Add Upsells & Bundle Offers',
			'margin:12px 0;width:fit-content;padding-right:50px;',
			false,
			array(
				'default' => 'Configure Offers',
				'loading' => 'Configuring...',
			)
		);
	}

	/**
	 * Render a notice below the coupons table on the Marketing > Coupons page.
	 *
	 * @return void
	 */
	public function render_coupons_page_notice() {
		global $typenow, $pagenow;

		if (
			'edit.php' !== $pagenow ||
			'shop_coupon' !== $typenow ||
			! $this->should_show_promotion( 'coupons_page' )
		) {
			return;
		}

		?>
		<div id="revx-promo-coupons-page-container" style="display:none;">
			<?php
			$this->render_promotion_notice(
				'revx-promo-coupons-page',
				'coupons_page',
				'Go Beyond Coupons with Smart Offers',
				'margin:20px 0 0; width: fit-content;padding-right:50px;',
				true,
				array(
					'default' => 'Set Up Offers',
					'loading' => 'Setting Up...',
				)
			);
			?>
		</div>
		<script>
			jQuery( function( $ ) {
				$(document).ready(function() {
					const $container = $( '#revx-promo-coupons-page-container' );
					const $tableNav = $( '#posts-filter' ).first();
					if ( $tableNav.length && $container.length ) {
						$container.insertAfter( $tableNav ).slideDown(300);
					}
				})
			} );
		</script>
		<?php
	}

	/**
	 * Render the promotional modal for the Products submenu item.
	 *
	 * @return void
	 */
	public function render_products_submenu_modal() {
		if ( ! is_admin() || ! current_user_can( 'edit_posts' ) ) {
			return;
		}

		$products_url = admin_url( 'edit.php?post_type=product' );
		$nonce        = wp_create_nonce( 'revx_promo_promotion_nonce' );
		$img_src      = ULTP_URL . '/assets/img/dashboard_banner/wooproduct/wowrevenue-modal.png'; // CHANGE THIS.
		?>
		<style>
			.revx-promo-products-modal {
				position: fixed;
				inset: 0;
				z-index: 99999;
				display: none;
				align-items: center;
				justify-content: center;
				padding: 24px;
				background: rgba(15, 23, 42, 0.62);
			}

			.revx-promo-products-modal.is-active {
				display: flex;
			}

			body.revx-promo-products-modal-open {
				overflow: hidden;
			}

			.revx-promo-products-modal *,
			.revx-promo-products-modal *::before,
			.revx-promo-products-modal *::after {
				box-sizing: border-box;
			}

			.revx-promo-products-modal__dialog {
				position: relative;
				width: min(100%, 470px);
				padding: 28px 28px 22px;
				border-radius: 22px;
				background: linear-gradient(180deg, #ffffff 0%, #f6f9ff 100%);
				box-shadow: 0 28px 80px rgba(15, 23, 42, 0.28);
				text-align: center;
			}

			.revx-promo-products-modal__close {
				position: absolute;
				top: 18px;
				right: 18px;
				border: 0;
				background: transparent;
				color: #64748b;
				cursor: pointer;
				font-size: 24px;
				line-height: 1;
			}

			.revx-promo-products-modal__close:hover,
			.revx-promo-products-modal__close:focus {
				color: #0f172a;
				outline: none;
			}

			.revx-promo-products-modal__title {
				margin: 0 0 10px;
				color: #16213e;
				font-size: 34px;
				line-height: 1.08;
				font-weight: 800;
				letter-spacing: -0.03em;
			}

			.revx-promo-products-modal__text {
				max-width: 330px;
				margin: 0 auto 20px;
				color: #64748b;
				font-size: 15px;
				line-height: 1.55;
			}

			.revx-promo-products-modal__list {
				margin: 0 auto 24px;
				padding: 0;
				/* max-width: 320px; */
				list-style: none;
				text-align: left;
			}

			.revx-promo-products-modal__list li {
				display: flex;
				align-items: center;
				gap: 12px;
				padding: 11px 0;
				border-bottom: 1px solid #e2e8f0;
				color: #1e293b;
				font-size: 14px;
				font-weight: 600;
			}

			.revx-promo-products-modal__list li:last-child {
				border-bottom: 0;
			}

			.revx-promo-products-modal__check {
				display: inline-flex;
				align-items: center;
				justify-content: center;
				width: 20px;
				height: 20px;
				border-radius: 50%;
				background: #dcfce7;
				color: #16a34a;
				font-size: 12px;
				font-weight: 700;
				flex: 0 0 20px;
			}

			.revx-promo-products-modal__button {
				display: flex;
				align-items: center;
				justify-content: center;
				gap: 10px;
				width: 100%;
				padding: 15px 18px;
				border: 0;
				border-radius: 10px;
				background: linear-gradient(180deg, #3b82f6 0%, #2563eb 100%);
				color: #fff;
				font-size: 15px;
				font-weight: 700;
				cursor: pointer;
				box-shadow: 0 14px 28px rgba(37, 99, 235, 0.24);
			}

			.revx-promo-products-modal__button:hover,
			.revx-promo-products-modal__button:focus {
				background: linear-gradient(180deg, #2563eb 0%, #1d4ed8 100%);
				color: #fff;
				outline: none;
			}

			.revx-promo-products-modal__button[aria-disabled="true"] {
				opacity: 0.75;
				cursor: wait;
			}

			.revx-promo-products-modal__learn {
				display: inline-block;
				margin-top: 12px;
				color: #2563eb;
				font-size: 13px;
				font-weight: 600;
				text-decoration: underline;
			}

			.revx-promo-products-modal__footer {
				display: grid;
				grid-template-columns: repeat(3, minmax(0, 1fr));
				gap: 10px;
				margin-top: 18px;
				padding: 12px;
				border: 1px solid #e2e8f0;
				border-radius: 14px;
				background: rgba(255, 255, 255, 0.85);
			}

			.revx-promo-products-modal__meta {
				display: flex;
				align-items: center;
				justify-content: center;
				gap: 6px;
				color: #64748b;
				font-size: 12px;
				font-weight: 600;
			}

			.revx-promo-products-modal__meta .dashicons {
				width: 16px;
				height: 16px;
				font-size: 16px;
			}

			@media (max-height: 760px) {
				.revx-promo-products-modal__dialog {
					padding: 22px 22px 18px;
				}

				.revx-promo-products-modal__img {
					max-height: 160px;
					margin-bottom: 18px;
				}

				.revx-promo-products-modal__title {
					font-size: 28px;
					margin-bottom: 8px;
				}

				.revx-promo-products-modal__text {
					margin-bottom: 16px;
					font-size: 14px;
					line-height: 1.45;
				}

				.revx-promo-products-modal__list {
					margin-bottom: 18px;
				}

				.revx-promo-products-modal__list li {
					gap: 10px;
					padding: 8px 0;
					font-size: 13px;
				}

				.revx-promo-products-modal__button {
					padding: 13px 16px;
					font-size: 14px;
				}
			}

			@media (max-height: 680px) {
				.revx-promo-products-modal__dialog {
					padding: 18px 18px 16px;
				}

				.revx-promo-products-modal__img {
					max-height: 120px;
					margin-bottom: 14px;
				}

				.revx-promo-products-modal__title {
					font-size: 24px;
				}

				.revx-promo-products-modal__text {
					max-width: 100%;
					margin-bottom: 12px;
					font-size: 13px;
				}

				.revx-promo-products-modal__list {
					margin-bottom: 14px;
				}

				.revx-promo-products-modal__list li {
					padding: 6px 0;
					font-size: 12px;
				}

				.revx-promo-products-modal__button {
					padding: 12px 14px;
					font-size: 13px;
				}
			}

			.revx-promo-products-modal__img {
				max-width: 100%;
				height: auto;
				margin-bottom: 24px;
				margin-inline: auto;
			}
		</style>

		<div id="revx-promo-products-modal" class="revx-promo-products-modal" aria-hidden="true">
			<div class="revx-promo-products-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="revx-promo-products-modal-title">
				<button type="button" class="revx-promo-products-modal__close" aria-label="Close">&times;</button>
				<img class="revx-promo-products-modal__img"  src="<?php echo esc_attr( $img_src ); ?>" />
				<h2 id="revx-promo-products-modal-title" class="revx-promo-products-modal__title">
					Create High-Converting Bundle Offers in Minutes
				</h2>
				<p class="revx-promo-products-modal__text">
					Bundle products, offer discounts, and increase your average order value - no setup hassle.
				</p>
				<ul class="revx-promo-products-modal__list">
					<li><span class="revx-promo-products-modal__check">&#10003;</span><span>
						Increase order value with smart, strategic bundles
					</span></li>
					<li><span class="revx-promo-products-modal__check">&#10003;</span><span>
						Turn browsers into buyers with irresistible discounts
					</span></li>
					<li><span class="revx-promo-products-modal__check">&#10003;</span><span>
						Seamlessly integrates with your WooCommerce store
					</span></li>
				</ul>
				<button
					type="button"
					class="revx-promo-products-modal__button"
					data-default-label="Start Creating Custom Options"
					data-loading-label="Starting..."
				>
					<span class="dashicons dashicons-admin-tools"></span>
					<span>Start Creating Bundle Offers</span>
				</button>
			</div>
		</div>

		<script>
			jQuery( function( $ ) {
				const modal = $( '#revx-promo-products-modal' );
				const dialog = modal.find( '.revx-promo-products-modal__dialog' );
				const button = modal.find( '.revx-promo-products-modal__button' );
				const buttonText = button.find( 'span' ).last();
				const submenuSlug = <?php echo wp_json_encode( self::PRODUCTS_MENU_SLUG ); ?>;
				const fallbackUrl = <?php echo wp_json_encode( $products_url ); ?>;
				const nonce = <?php echo wp_json_encode( $nonce ); ?>;
				const dashboardUrl = <?php echo wp_json_encode( $this->get_dashboard_url() ); ?>;
				const defaultErrorMessage = <?php echo wp_json_encode( 'Failed to install WowAddons.' ); ?>;
				let openedFromModalPage = window.location.search.indexOf( 'page=<?php echo esc_js( self::PRODUCTS_MENU_SLUG ); ?>' ) !== -1;

				function openModal() {
					modal.addClass( 'is-active' ).attr( 'aria-hidden', 'false' );
					$( 'body' ).addClass( 'revx-promo-products-modal-open' );
				}

				function closeModal() {
					modal.removeClass( 'is-active' ).attr( 'aria-hidden', 'true' );
					$( 'body' ).removeClass( 'revx-promo-products-modal-open' );

					if ( openedFromModalPage ) {
						window.location.href = fallbackUrl;
					}
				}

				$( document ).on( 'click', '#adminmenu a[href*="page=' + submenuSlug + '"], .wp-submenu a[href*="page=' + submenuSlug + '"]', function( event ) {
					event.preventDefault();
					event.stopImmediatePropagation();
					openedFromModalPage = false;
					openModal();
					return false;
				} );

				modal.on( 'click', function( event ) {
					if ( ! dialog.is( event.target ) && 0 === dialog.has( event.target ).length ) {
						closeModal();
					}
				} );

				modal.find( '.revx-promo-products-modal__close' ).on( 'click', function() {
					closeModal();
				} );

				$( document ).on( 'keydown', function( event ) {
					if ( 'Escape' === event.key && modal.hasClass( 'is-active' ) ) {
						closeModal();
					}
				} );

				button.on( 'click', function( event ) {
					event.preventDefault();

					if ( 'true' === button.attr( 'aria-disabled' ) ) {
						return;
					}

					button.attr( 'aria-disabled', 'true' );
					buttonText.text( button.data( 'loading-label' ) );

					$.ajax( {
						url: <?php echo wp_json_encode( admin_url( 'admin-ajax.php' ) ); ?>,
						type: 'POST',
						dataType: 'json',
						data: {
							action: 'revx_promo_install_promotion_plugin',
							nonce: nonce
						}
					} ).done( function( response ) {
						if ( ! response || ! response.success ) {
							button.attr( 'aria-disabled', 'false' );
							buttonText.text( button.data( 'default-label' ) );
							window.alert( defaultErrorMessage );
							return;
						}

						window.location.href = dashboardUrl;
					} ).fail( function( xhr ) {
						const message = xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message
							? xhr.responseJSON.data.message
							: defaultErrorMessage;

						button.attr( 'aria-disabled', 'false' );
						buttonText.text( button.data( 'default-label' ) );
						window.alert( message );
					} );
				} );

				if ( openedFromModalPage ) {
					openModal();
				}
			} );
		</script>
		<?php
	}

	/**
	 * Ajax handler for dismissing promotion notice.
	 *
	 * @return void
	 */
	public function ajax_dismiss_promotion() {
		check_ajax_referer( 'revx_promo_promotion_nonce', 'nonce' );

		$type = sanitize_text_field( wp_unslash( $_POST['type'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$this->set_dismissed( $type );

		wp_send_json_success();
	}

	/**
	 * Ajax handler for installing the promoted plugin.
	 *
	 * @return void
	 */
	public function ajax_install_promotion_plugin() {
		check_ajax_referer( 'revx_promo_promotion_nonce', 'nonce' );

		$plugin_exists = file_exists( WP_PLUGIN_DIR . '/' . self::PROMOTED_PLUGIN_FILE );

		if ( ! $plugin_exists && ! current_user_can( 'install_plugins' ) ) {
			wp_send_json_error(
				array(
					'message' => 'You are not allowed to install plugins.',
				)
			);
		}

		if ( $plugin_exists && ! current_user_can( 'activate_plugins' ) ) {
			wp_send_json_error(
				array(
					'message' => 'You are not allowed to activate plugins.',
				)
			);
		}

		$result = $this->install_and_active_plugin();

		if ( false === $result ) {
			wp_send_json_error(
				array(
					'message' => 'Failed to install WowAddons.',
				)
			);
		}

		wp_send_json_success(
			array(
				'status'        => $result,
				'dashboard_url' => $this->get_dashboard_url(),
				'message'       => 'WowAddons installed successfully.',
			)
		);
	}

	/**
	 * Get dismiss key
	 *
	 * @param string $type Promotion type.
	 * @return string
	 */
	private function get_dismiss_key( $type ) {
		return 'revx_promo_promotion_is_closed_' . self::VERSION . '_' . $type;
	}

	/**
	 * Should show a promotion
	 *
	 * @param string $type Promotion type.
	 * @return void
	 */
	private function set_dismissed( $type ) {
		set_transient( $this->get_dismiss_key( $type ), 'yes', DAY_IN_SECONDS * 30 );
	}

	/**
	 * Should show a promotion
	 * Dont show promotions if:
	 * - The promotion was dismissed by the user.
	 * - The promotion hook already ran in the current page load by another plugin.
	 *
	 * @param string $type Promotion type.
	 * @return boolean
	 */
	private function should_show_promotion( $type ) {
		$ran_once = boolval( $GLOBALS['revx_promo_promotion'][ $type ] ?? false );
		if ( $ran_once ) {
			return false;
		}
		return get_transient( $this->get_dismiss_key( $type ) ) !== 'yes';
	}

	/**
	 * Render a reusable promotion notice block.
	 *
	 * @param string  $id      Notice DOM id.
	 * @param string  $type    Promotion type.
	 * @param string  $message Notice message.
	 * @param string  $style   Inline wrapper style.
	 * @param boolean $inline Whether the notice should use inline positioning classes.
	 * @param array   $button_labels Button text overrides.
	 * @param string  $container_cls Additional inline styles for the inner container.
	 * @return void
	 */
	private function render_promotion_notice( $id, $type, $message, $style = '', $inline = true, $button_labels = array(), $container_cls = '' ) {
		$GLOBALS['revx_promo_promotion'][ $type ] = true;

		$button_labels = wp_parse_args(
			is_array( $button_labels ) ? $button_labels : array(),
			array(
				'default' => 'Start Now',
				'loading' => 'Starting...',
			)
		);

		$classes = array( 'notice', 'notice-info', 'is-dismissible', 'wtrs-promotion-notice' );

		if ( $inline ) {
			$classes[] = 'inline';
		}

		ob_start();
		?>

		<div 
			id="<?php echo esc_attr( $id ); ?>" 
			class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" 
			data-type="<?php echo esc_attr( $type ); ?>"
			style="<?php echo esc_attr( $style ); ?>"
		>
			<div style="padding-block:12px;display:flex;gap:1rem;align-items:center;<?php echo esc_attr( $container_cls ); ?>">
				<span>
					<?php echo wp_kses_post( $message ); ?>
				</span>
				<a
					href="#"
					class="button button-secondary wtrs-promotion-install-link"
					role="button"
					data-default-label="<?php echo esc_attr( $button_labels['default'] ); ?>"
					data-loading-label="<?php echo esc_attr( $button_labels['loading'] ); ?>"
				>
					<?php echo esc_html( $button_labels['default'] ); ?>
				</a>
			</div>
			<button type="button" class="notice-dismiss">
				<span class="screen-reader-text">Dismiss this notice</span>
			</button>
		</div>
		<?php $this->echo_dismiss_notice_js( '#' . $id ); ?>
		<?php $this->echo_install_notice_js( '#' . $id ); ?>
		<?php
		echo ob_get_clean(); // phpcs:ignore
	}

	/**
	 * Notice dismiss js
	 *
	 * @param string $id Notice ID.
	 * @return void
	 */
	private function echo_dismiss_notice_js( $id ) {
		ob_start();
		?>
		<script>
			jQuery( function( $ ) {
				$( document ).on( 'click', '<?php echo esc_js( $id ); ?> .notice-dismiss', function() {
					var $notice = $( this ).closest( '<?php echo esc_js( $id ); ?>' );

					if ( ! $notice.length || 'true' === $notice.attr( 'data-dismissed' ) ) {
						return;
					}

					$notice.slideUp( 300, function() {
						$notice.remove();
					} ).attr( 'data-dismissed', 'true' );

					$.ajax( {
						url: <?php echo wp_json_encode( admin_url( 'admin-ajax.php' ) ); ?>,
						type: 'POST',
						data: {
							action: 'revx_promo_dismiss_promotion',
							nonce: <?php echo wp_json_encode( wp_create_nonce( 'revx_promo_promotion_nonce' ) ); ?>,
							type: $notice.data( 'type' ) || ''
						}
					} )
				} );
			} );
		</script>
		<?php
		echo ob_get_clean(); // phpcs:ignore
	}

	/**
	 * Notice install js.
	 *
	 * @param string $id Notice ID.
	 * @return void
	 */
	private function echo_install_notice_js( $id ) {
		ob_start();
		?>
		<script>
			jQuery( function( $ ) {
				$( document ).on( 'click', '<?php echo esc_js( $id ); ?> .wtrs-promotion-install-link', function( event ) {
					var $button = $( this );
					var $notice = $button.closest( '<?php echo esc_js( $id ); ?>' );
					var defaultErrorMessage = <?php echo wp_json_encode( 'Failed to install WowAddons.' ); ?>;

					event.preventDefault();

					if ( ! $notice.length || 'true' === $button.attr( 'aria-disabled' ) ) {
						return;
					}

					$button.attr( 'aria-disabled', 'true' )
					.addClass( 'button-disabled' )
					.text($button.data( 'loading-label' ))

					$.ajax( {
						url: <?php echo wp_json_encode( admin_url( 'admin-ajax.php' ) ); ?>,
						type: 'POST',
						dataType: 'json',
						data: {
							action: 'revx_promo_install_promotion_plugin',
							nonce: <?php echo wp_json_encode( wp_create_nonce( 'revx_promo_promotion_nonce' ) ); ?>
						}
					} ).done( function( response ) {
						if ( ! response || ! response.success || ! response.data ) {
							$button.attr( 'aria-disabled', 'false' ).removeClass( 'button-disabled' ).text( $button.data( 'default-label' ) );
							window.alert( defaultErrorMessage );
							return;
						}
						window.location.href = "<?php echo esc_js( $this->get_dashboard_url() ); ?>";
					} ).fail( function( xhr ) {
						var message = xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message
							? xhr.responseJSON.data.message
							: defaultErrorMessage;

						$button.attr( 'aria-disabled', 'false' ).removeClass( 'button-disabled' ).text( $button.data( 'default-label' ) );
						window.alert( message );
					} );
				} );
			} );
		</script>
		<?php
		echo ob_get_clean(); // phpcs:ignore
	}

	/**
	 * Get the dashboard URL for the promoted plugin.
	 *
	 * @return string
	 */
	private function get_dashboard_url() {
		return admin_url( 'admin.php?page=revenue#/' );
	}

	/**
	 * Installs and activates the promoted plugin.
	 *
	 * @return string|false
	 */
	public function install_and_active_plugin() {
		include_once ABSPATH . 'wp-admin/includes/plugin.php';

		if ( is_plugin_active( self::PROMOTED_PLUGIN_FILE ) ) {
			return 'active';
		}

		if ( ! file_exists( WP_PLUGIN_DIR . '/' . self::PROMOTED_PLUGIN_FILE ) ) {
			if ( ! $this->download_plugin( self::PROMOTED_PLUGIN_FILE, self::PROMOTED_PLUGIN_SLUG ) ) {
				return false;
			}
		}

		$res = activate_plugin( self::PROMOTED_PLUGIN_FILE );

		return is_wp_error( $res ) ? false : 'installed';
	}

	/**
	 * Installs a plugin based on the provided plugin file and slug.
	 *
	 * This function is expected to handle the logic required to install a plugin,
	 * such as downloading, unpacking, and activating the plugin using the provided
	 * plugin file and slug.
	 *
	 * @param string $plugin The plugin file path or identifier (e.g., 'plugin-directory/plugin-file.php').
	 * @param string $slug   The plugin slug (typically the directory name of the plugin).
	 */
	private function download_plugin( $plugin, $slug ) {
		include ABSPATH . 'wp-admin/includes/plugin-install.php';
		include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

		if ( ! class_exists( 'Plugin_Upgrader' ) ) {
			include ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
		}
		if ( ! class_exists( 'WP_Ajax_Upgrader_Skin' ) ) {
			include ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
		}

		$api = plugins_api(
			'plugin_information',
			array(
				'slug'   => $slug,
				'fields' => array(
					'short_description' => false,
					'sections'          => false,
					'requires'          => false,
					'rating'            => false,
					'ratings'           => false,
					'downloaded'        => false,
					'last_updated'      => false,
					'added'             => false,
					'tags'              => false,
					'compatibility'     => false,
					'homepage'          => false,
					'donate_link'       => false,
				),
			)
		);

		if ( is_wp_error( $api ) ) {
			return false;
		}

		$upgrader       = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() );
		$install_result = $upgrader->install( $api->download_link );

		return is_wp_error( $install_result ) || false === $install_result ? false : true;
	}
}
