<?php
/**
 * Admin promo banner for pro theme upgrade.
 *
 * Shown only on Appearance > Themes screen.
 * Dismissible per-user (persisted via user meta).
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

define( 'SWP_NOTARY_PROMO_META_KEY', 'swp_notary_promo_dismissed' );
define( 'SWP_NOTARY_PROMO_NONCE', 'swp_notary_promo_dismiss' );

/**
 * Render banner on Appearance > Themes screen.
 */
function swp_notary_admin_promo_banner() {

	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	$screen = get_current_screen();
	if ( ! $screen || 'themes' !== $screen->id ) {
		return;
	}

	if ( get_user_meta( get_current_user_id(), SWP_NOTARY_PROMO_META_KEY, true ) ) {
		return;
	}

	$logo_url = get_theme_file_uri( '/assets/images/admin-banner/scintillawp-logo.webp' );
	$buy_url  = 'https://scintillawpthemes.com/product/notary-wordpress-theme/?utm_source=wordpress&utm_medium=theme&utm_campaign=free_theme_admin_banner';
	$demo_url = 'https://scintillawpthemes.com/demo/swp-notary-pro/?utm_source=wordpress&utm_medium=theme&utm_campaign=free_theme_admin_banner';
	$nonce    = wp_create_nonce( SWP_NOTARY_PROMO_NONCE );
	?>
	<div class="swp-notary-promo-banner" data-nonce="<?php echo esc_attr( $nonce ); ?>">
		<div class="swp-notary-promo-banner__inner">
			<div class="swp-notary-promo-banner__brand">
				<img src="<?php echo esc_url( $logo_url ); ?>" alt="<?php esc_attr_e( 'ScintillaWP', 'swp-notary' ); ?>" class="swp-notary-promo-banner__logo" />
			</div>
			<div class="swp-notary-promo-banner__content">
				<h2 class="swp-notary-promo-banner__title"><?php esc_html_e( 'Upgrade to SWP Notary Pro', 'swp-notary' ); ?></h2>
				<p class="swp-notary-promo-banner__text"><?php esc_html_e( 'Unlock premium patterns, advanced layouts, and priority support built for notary & legal professionals.', 'swp-notary' ); ?></p>
			</div>
			<div class="swp-notary-promo-banner__actions">
				<a href="<?php echo esc_url( $buy_url ); ?>" class="swp-notary-promo-banner__btn swp-notary-promo-banner__btn--buy" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Buy Now', 'swp-notary' ); ?></a>
				<a href="<?php echo esc_url( $demo_url ); ?>" class="swp-notary-promo-banner__btn swp-notary-promo-banner__btn--demo" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'View Demo', 'swp-notary' ); ?></a>
			</div>
		</div>
		<button type="button" class="swp-notary-promo-banner__close" aria-label="<?php esc_attr_e( 'Dismiss this notice', 'swp-notary' ); ?>">
			<span aria-hidden="true">&times;</span>
		</button>
	</div>
	<?php
}
add_action( 'admin_notices', 'swp_notary_admin_promo_banner' );

/**
 * Enqueue banner CSS + JS only on Themes screen.
 */
function swp_notary_admin_promo_banner_assets( $hook ) {

	if ( 'themes.php' !== $hook ) {
		return;
	}

	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	if ( get_user_meta( get_current_user_id(), SWP_NOTARY_PROMO_META_KEY, true ) ) {
		return;
	}

	wp_enqueue_style(
		'swp-notary-promo-banner',
		get_theme_file_uri( '/assets/css/admin-promo-banner.css' ),
		array(),
		wp_get_theme()->get( 'Version' )
	);

	wp_enqueue_script(
		'swp-notary-promo-banner',
		get_theme_file_uri( '/assets/js/admin-promo-banner.js' ),
		array( 'jquery' ),
		wp_get_theme()->get( 'Version' ),
		true
	);

	wp_localize_script(
		'swp-notary-promo-banner',
		'swpNotaryPromo',
		array(
			'ajaxUrl' => admin_url( 'admin-ajax.php' ),
			'action'  => 'swp_notary_dismiss_promo',
		)
	);
}
add_action( 'admin_enqueue_scripts', 'swp_notary_admin_promo_banner_assets' );

/**
 * AJAX handler: persist per-user dismissal.
 */
function swp_notary_dismiss_promo_handler() {

	if ( ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
	}

	check_ajax_referer( SWP_NOTARY_PROMO_NONCE, 'nonce' );

	update_user_meta( get_current_user_id(), SWP_NOTARY_PROMO_META_KEY, 1 );

	wp_send_json_success();
}
add_action( 'wp_ajax_swp_notary_dismiss_promo', 'swp_notary_dismiss_promo_handler' );
