<?php
/**
 * Admin notice helper class.
 *
 * Provides methods for displaying WordPress admin notices with proper escaping.
 *
 * @package Starter
 * @since 2.0.0
 */

namespace Storeplugin\PDFInvoice\Framework\Utils;

defined('ABSPATH') || exit;

use Storeplugin\PDFInvoice\Config;

/**
 * Admin notice helper class.
 *
 * @since 1.0.0
 */
final class AdminNotice {
	/**
	 * Display an error notice.
	 *
	 * @param string $message The message to display (can contain safe HTML).
	 * @param bool   $debugOnly Only show in debug mode.
	 * @return void
	 * @since 1.0.0
	 */
	public static function error(string $message, bool $debugOnly = false): void {
		self::addNotice($message, 'error', $debugOnly);
	}

	/**
	 * Display a warning notice.
	 *
	 * @param string $message The message to display (can contain safe HTML).
	 * @param bool   $debugOnly Only show in debug mode.
	 * @return void
	 * @since 1.0.0
	 */
	public static function warning(string $message, bool $debugOnly = true): void {
		self::addNotice($message, 'warning', $debugOnly);
	}

	/**
	 * Display a success notice.
	 *
	 * @param string $message The message to display (can contain safe HTML).
	 * @param bool   $debugOnly Only show in debug mode.
	 * @return void
	 * @since 1.0.0
	 */
	public static function success(string $message, bool $debugOnly = false): void {
		self::addNotice($message, 'success', $debugOnly);
	}

	/**
	 * Display an info notice.
	 *
	 * @param string $message The message to display (can contain safe HTML).
	 * @param bool   $debugOnly Only show in debug mode.
	 * @return void
	 * @since 1.0.0
	 */
	public static function info(string $message, bool $debugOnly = false): void {
		self::addNotice($message, 'info', $debugOnly);
	}

	/**
	 * Add a notice to the admin notices queue.
	 *
	 * @param string $message The message to display.
	 * @param string $type Notice type (error, warning, success, info).
	 * @param bool   $debugOnly Only show in debug mode.
	 * @return void
	 * @since 1.0.0
	 */
	private static function addNotice(string $message, string $type, bool $debugOnly): void {
		add_action('admin_notices', function() use ($message, $type, $debugOnly) {
			if ($debugOnly && !Utils::isDebugMode()) {
				return;
			}

			$allowedTags = [
				'code'   => [],
				'strong' => [],
				'em'     => [],
				'br'     => [],
				'small'  => [],
			];

			printf(
				'<div class="notice notice-%s"><p><strong>%s:</strong> %s</p></div>',
				esc_attr($type),
				esc_html(Config::title()),
				wp_kses($message, $allowedTags)
			);
			});
		}
	}
