<?php
/**
 * Dismissible cache-plugin compatibility notice for metabox + sitemap screens.
 *
 * @package SEOvaultSEO\Admin
 */

namespace SEOvaultSEO\Admin;

use SEOvaultSEO\Utils\CachePlugins;

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

/**
 * Renders and enqueues the cache compatibility notice.
 */
class CacheCompatibilityNotice {

	/**
	 * localStorage key shared across screens (spec).
	 */
	const DISMISS_STORAGE_KEY = 'seovault_cache_notice_dismissed';

	/**
	 * Script handle.
	 */
	const SCRIPT_HANDLE = 'seovaultseo-cache-notice';

	/**
	 * Whether the script was already enqueued this request.
	 *
	 * @var bool
	 */
	private static $enqueued = false;

	/**
	 * Enqueue dismiss script when a cache plugin is active.
	 */
	public static function enqueue() {
		if ( self::$enqueued || ! CachePlugins::is_active() ) {
			return;
		}

		self::$enqueued = true;

		wp_enqueue_script(
			self::SCRIPT_HANDLE,
			SEOVAULT_SEO_URL . 'assets/js/cache-compatibility-notice.js',
			array(),
			SEOVAULT_SEO_VERSION . '-1',
			true
		);

		wp_localize_script(
			self::SCRIPT_HANDLE,
			'seovaultseo_cache_notice',
			array(
				'dismissKey' => self::DISMISS_STORAGE_KEY,
				'i18n'       => array(
					'dismiss' => __( 'Dismiss notice', 'seovault-ai' ),
				),
			)
		);
	}

	/**
	 * Data suitable for other script localizations.
	 *
	 * @return array{active: bool, plugins: array<int, string>, dismissKey: string}
	 */
	public static function get_localize_data() {
		$plugins = CachePlugins::get_active();

		return array(
			'active'     => ! empty( $plugins ),
			'plugins'    => $plugins,
			'dismissKey' => self::DISMISS_STORAGE_KEY,
		);
	}

	/**
	 * Render the notice markup (hidden later by JS if dismissed).
	 *
	 * @param bool $compact Use the compact single-line variant (metabox).
	 */
	public static function render( $compact = false ) {
		if ( ! CachePlugins::is_active() ) {
			return;
		}

		self::enqueue();

		$plugins = CachePlugins::get_active();
		$names   = implode( ', ', $plugins );

		$compact_message = __( 'A cache plugin may delay when your SEO changes go live. If updates do not appear, purge your cache.', 'seovault-ai' );
		$title           = __( 'Cache plugin detected', 'seovault-ai' );
		$full_message    = __( 'A cache plugin is active on your site. Cached pages may delay SEO meta and sitemap updates. If you do not see your latest changes, purge your cache (or exclude SEO/sitemap URLs from caching).', 'seovault-ai' );

		$classes = 'seovaultseo-cache-notice';
		if ( $compact ) {
			$classes .= ' seovaultseo-cache-notice--compact';
		}

		?>
		<div class="<?php echo esc_attr( $classes ); ?>" role="status" data-seovault-cache-notice="1">
			<div class="seovaultseo-cache-notice__body">
				<?php if ( $compact ) : ?>
					<span class="dashicons dashicons-warning" aria-hidden="true"></span>
					<p class="seovaultseo-cache-notice__message">
						<?php echo esc_html( $compact_message ); ?>
						<?php if ( '' !== $names ) : ?>
							<span class="seovaultseo-cache-notice__plugins">(<?php echo esc_html( $names ); ?>)</span>
						<?php endif; ?>
					</p>
				<?php else : ?>
					<strong class="seovaultseo-cache-notice__title"><?php echo esc_html( $title ); ?></strong>
					<p class="seovaultseo-cache-notice__message">
						<?php echo esc_html( $full_message ); ?>
						<?php if ( '' !== $names ) : ?>
							<span class="seovaultseo-cache-notice__plugins"><?php echo esc_html( $names ); ?></span>
						<?php endif; ?>
					</p>
				<?php endif; ?>
			</div>
			<button type="button" class="seovaultseo-cache-notice__dismiss" aria-label="<?php esc_attr_e( 'Dismiss notice', 'seovault-ai' ); ?>">
				<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
			</button>
		</div>
		<?php
	}
}
