<?php
/**
 * Admin welcome notice on theme activation.
 *
 * @package storecraft
 */

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

define( 'STORECRAFT_BUY_NOW', 'https://hudawp.com/product/storecraft-multipurpose-woocommerce-wordpress-theme/' );
define( 'STORECRAFT_DOC_URL', 'https://demo.hudawp.com/storecraft/' );

/**
 * Enqueue admin notice assets.
 */
function storecraft_admin_notice_assets() {
	if ( get_user_meta( get_current_user_id(), 'storecraft_dismissed_notice', true ) ) {
		return;
	}

	wp_enqueue_style( 'dashicons' );

	wp_enqueue_style(
		'storecraft-admin-notice-style',
		get_template_directory_uri() . '/get-started/getstart.css',
		array(),
		wp_get_theme()->get( 'Version' )
	);

	wp_enqueue_script(
		'storecraft-admin-notice-script',
		get_template_directory_uri() . '/get-started/js/admin-notice-script.js',
		array( 'jquery' ),
		wp_get_theme()->get( 'Version' ),
		true
	);
}
add_action( 'admin_enqueue_scripts', 'storecraft_admin_notice_assets' );

/**
 * Theme features for the admin welcome notice.
 *
 * @return string[]
 */
function storecraft_get_theme_features() {
	return array(
		__( 'Multi-Currency Switcher', 'storecraft' ),
		__( 'AJAX Add to Cart', 'storecraft' ),
		__( 'Smart Wishlist', 'storecraft' ),
		__( 'Free Shipping Bar/Nudge', 'storecraft' ),
		__( 'Mega Menu Support', 'storecraft' ),
		__( 'Core Web Vitals Optimized', 'storecraft' ),
		__( 'Multiple Widget Areas', 'storecraft' ),
		__( 'SEO Friendly Structure', 'storecraft' ),
		__( 'And More Features', 'storecraft' ),
	);
}

/**
 * Display the admin welcome banner.
 */
function storecraft_admin_notice() {
	if ( get_user_meta( get_current_user_id(), 'storecraft_dismissed_notice', true ) ) {
		return;
	}

	?>
	<div class="updated notice notice-success is-dismissible notice-get-started-class" data-notice="storecraft-get-start" style="border: 2px solid #b5fd57; display: flex; padding: 35px;background-color: #fff;">
		<div class="notice-content">
			<div class="notice-holder">
				<h5><span class="theme-name"><?php esc_html_e( 'Upgrade To Premium', 'storecraft' ); ?></span></h5>
				<h1><?php esc_html_e( 'Unlock More Power With Storecraft Premium!', 'storecraft' ); ?></h1>
				<div class="notice-text">
					<p class="blocks-text"><?php esc_html_e( 'Build a stunning fashion & WooCommerce store with StoreCraft! Enjoy seamless performance, responsive designs, and effortless customization. Create your perfect online store and grow your business with ease.', 'storecraft' ); ?></p>
				</div>
				<a href="<?php echo esc_url( admin_url( 'customize.php' ) ); ?>" id="storecraft-get-started-button" class="button admin-button info-button">
					<?php esc_html_e( 'Getting started', 'storecraft' ); ?>
				</a>
				<a href="<?php echo esc_url( STORECRAFT_DOC_URL ); ?>" target="_blank" rel="noopener noreferrer" id="storecraft-doc-button" class="button admin-button bundle-button">
					<?php esc_html_e( 'View Demo', 'storecraft' ); ?>
				</a>
				<a href="<?php echo esc_url( STORECRAFT_BUY_NOW ); ?>" target="_blank" rel="noopener noreferrer" id="storecraft-go-pro-button" class="button admin-button buy-now-button">
					<?php esc_html_e( 'Buy Now Premium Theme', 'storecraft' ); ?>
				</a>
			</div>
			<div class="notice-features">
				<h3 class="notice-features-title"><?php esc_html_e( 'Theme Features', 'storecraft' ); ?></h3>
				<ul class="notice-features-list">
					<?php foreach ( storecraft_get_theme_features() as $storecraft_feature ) : ?>
						<li>
							<span class="dashicons dashicons-yes-alt" aria-hidden="true"></span>
							<?php echo esc_html( $storecraft_feature ); ?>
						</li>
					<?php endforeach; ?>
				</ul>
			</div>
		</div>
		<div class="theme-hero-screens">
			<img src="<?php echo esc_url( get_template_directory_uri() . '/screenshot.png' ); ?>" alt="<?php esc_attr_e( 'Storecraft theme screenshot', 'storecraft' ); ?>" />
		</div>
	</div>
	<?php
}
add_action( 'admin_notices', 'storecraft_admin_notice' );

/**
 * AJAX handler to dismiss the notice.
 */
function storecraft_dismissed_notice() {
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( -1 );
	}

	update_user_meta( get_current_user_id(), 'storecraft_dismissed_notice', true );
	wp_die();
}
add_action( 'wp_ajax_storecraft_dismissed_notice', 'storecraft_dismissed_notice' );

/**
 * Show the notice again when the theme is activated.
 */
function storecraft_switch_theme() {
	delete_user_meta( get_current_user_id(), 'storecraft_dismissed_notice' );
}
add_action( 'after_switch_theme', 'storecraft_switch_theme' );
