<?php
if ( ! defined( 'ABSPATH' ) ) exit;

	/**
	 * Handles the display of a dismissible admin notice for premium features.
	 */
	class Sifency_Unlock_Notice {
		const DISMISS_META_KEY = '_sifency_dismissed_unlock_notice';
		public function __construct() {
			add_action( 'admin_init', [ $this, 'handle_dismiss_action' ] );
			add_action( 'admin_notices', [ $this, 'display_notice' ] );
		}

		public function handle_dismiss_action(): void {
			if ( isset( $_GET['sifency_dismiss_notice'], $_GET['_wpnonce'] ) ) {
				if ( wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'sifency_dismiss_unlock_notice_nonce' ) ) {
					update_user_meta( get_current_user_id(), self::DISMISS_META_KEY, true );
				}
			}
		}

		public function display_notice(): void {
			if ( get_user_meta( get_current_user_id(), self::DISMISS_META_KEY, true ) ) return;
			if ( Sifency_Addons_Theme_Builder_Master::sifency_addons_is_legal() ) return;
			$screen = get_current_screen();
			if ( ! $screen || in_array($screen->id, ['toplevel_page_sifency-theme-builder', 'sifency_page_sifency-theme-builder']) ) return;
			$dismiss_url = wp_nonce_url( add_query_arg( 'sifency_dismiss_notice', '1' ), 'sifency_dismiss_unlock_notice_nonce' );
			?>
			<div class="notice notice-info is-dismissible">
				<p>
					<?php
					$premium_themes_link = sprintf(
						'<a href="%s" target="_blank">%s</a>',
						'https://sifency.com',
						esc_html__( 'premium themes', 'sifency-addons' )
					);
					$message = sprintf(
						/* translators: %s: HTML link to the premium themes page. */
						__( '<strong>Unlock the full power of Sifency Theme Builder!</strong> Get access to advanced templates by using one of our %s.', 'sifency-addons' ),
						$premium_themes_link
					);
					echo wp_kses_post( $message );
					?>
					<a href="<?php echo esc_url( $dismiss_url ); ?>" style="text-decoration:none; margin-left: 10px;" aria-label="<?php esc_attr_e( 'Dismiss this notice permanently', 'sifency-addons' ); ?>">
						<?php esc_html_e( 'Dismiss Permanently', 'sifency-addons' ); ?>
					</a>
				</p>
			</div>
			<?php
		}

	}