<?php
/**
 * Admin promo banner for pro theme upgrade.
 *
 * Shown on all admin screens.
 * Dismissible per-user (persisted via user meta).
 */

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

define( 'SWP_PAINTING_PROMO_META_KEY', 'swp_painting_promo_dismissed' );
define( 'SWP_PAINTING_PROMO_NONCE', 'swp_painting_promo_dismiss' );
define( 'SWP_PAINTING_PROMO_SNOOZE', 5 * MINUTE_IN_SECONDS );

/**
 * Whether the banner is currently dismissed (within the snooze window).
 */
function swp_painting_promo_is_dismissed() {
	$dismissed_at = (int) get_user_meta( get_current_user_id(), SWP_PAINTING_PROMO_META_KEY, true );

	return $dismissed_at && ( time() - $dismissed_at ) < SWP_PAINTING_PROMO_SNOOZE;
}

/**
 * Render banner on all admin screens.
 */
function swp_painting_admin_promo_banner() {

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

	if ( swp_painting_promo_is_dismissed() ) {
		return;
	}

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

/**
 * Enqueue banner CSS + JS on all admin screens.
 */
function swp_painting_admin_promo_banner_assets( $hook ) {

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

	if ( swp_painting_promo_is_dismissed() ) {
		return;
	}

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

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

	wp_localize_script(
		'swp-painting-promo-banner',
		'swpPaintingPromo',
		array(
			'ajaxUrl' => admin_url( 'admin-ajax.php' ),
			'action'  => 'swp_painting_dismiss_promo',
		)
	);
}
add_action( 'admin_enqueue_scripts', 'swp_painting_admin_promo_banner_assets' );

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

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

	check_ajax_referer( SWP_PAINTING_PROMO_NONCE, 'nonce' );

	update_user_meta( get_current_user_id(), SWP_PAINTING_PROMO_META_KEY, time() );

	wp_send_json_success();
}
add_action( 'wp_ajax_swp_painting_dismiss_promo', 'swp_painting_dismiss_promo_handler' );
