<?php
/**
 * Admin Notices Handler
 *
 * @package Agency_Pulse_Admin_Menu
 * @since 1.0.0
 */

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

/**
 * Class Agency_Pulse_Admin_Menu_Notices
 *
 * Queues and renders WordPress admin notices. Mirrors the notices pattern
 * used in agency-pulse-invoice so external code can call ::add() early.
 *
 * @since 1.0.0
 */
class Agency_Pulse_Admin_Menu_Notices {

	/**
	 * Queued notices waiting to be rendered.
	 *
	 * @var array
	 */
	private static $notices = array();

	/**
	 * Queue a notice for display.
	 *
	 * @since 1.0.0
	 * @param string $message  The notice message (may contain HTML).
	 * @param string $type     Notice type: 'success', 'error', 'warning', 'info'.
	 * @param bool   $dismissible Whether the notice can be dismissed.
	 */
	public static function add( $message, $type = 'info', $dismissible = true ) {
		self::$notices[] = array(
			'message'     => $message,
			'type'        => $type,
			'dismissible' => $dismissible,
		);

		// Ensure the render callback is only hooked once.
		if ( ! has_action( 'admin_notices', array( __CLASS__, 'render' ) ) ) {
			add_action( 'admin_notices', array( __CLASS__, 'render' ) );
		}
	}

	/**
	 * Render all queued notices.
	 *
	 * @since 1.0.0
	 */
	public static function render() {
		foreach ( self::$notices as $notice ) {
			$classes  = 'notice notice-' . esc_attr( $notice['type'] );
			$classes .= $notice['dismissible'] ? ' is-dismissible' : '';
			printf(
				'<div class="%s"><p>%s</p></div>',
				esc_attr( $classes ),
				wp_kses_post( $notice['message'] )
			);
		}
		self::$notices = array();
	}

	/**
	 * Queue a success notice.
	 *
	 * @since 1.0.0
	 * @param string $message
	 */
	public static function success( $message ) {
		self::add( $message, 'success' );
	}

	/**
	 * Queue an error notice.
	 *
	 * @since 1.0.0
	 * @param string $message
	 */
	public static function error( $message ) {
		self::add( $message, 'error' );
	}

	/**
	 * Queue admin notices for actionable License Manager states.
	 *
	 * Intended to be hooked to admin_init so the notice is queued before
	 * admin_notices fires. Only shows notices on plugin admin pages.
	 *
	 * @since 1.0.0
	 */
	public static function maybe_display_license_notices(): void {
		// Only show on plugin pages to avoid cluttering every admin screen.
		$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$plugin_pages = array(
			Agency_Pulse_Admin_Menu_Constants::PAGE_MENU_BUILDER,
			Agency_Pulse_Admin_Menu_Constants::PAGE_SETTINGS,
			Agency_Pulse_Admin_Menu_Constants::PAGE_ROLE_ASSIGNMENTS,
		);
		if ( ! in_array( $page, $plugin_pages, true ) ) {
			return;
		}

		$status = Agency_Pulse_Admin_Menu_License_Manager::get_state()['status'];
		$settings_url = admin_url( 'admin.php?page=' . Agency_Pulse_Admin_Menu_Constants::PAGE_SETTINGS );

		if ( 'pending_email' === $status ) {
			self::add(
				sprintf(
					/* translators: %s: link to Settings page */
					__( '<strong>Menu Organizer:</strong> An authorization email has been sent. Click the link in your inbox, then <a href="%s">return to Settings</a> and click "I\'ve Confirmed — Check Connection".', 'agency-pulse-admin-menu' ),
					esc_url( $settings_url )
				),
				'warning',
				true
			);
		}

		if ( 'slot_full' === $status ) {
			self::add(
				__( '<strong>Menu Organizer:</strong> All domain slots for this license are in use. Release a slot at <a href="https://agencypulsewp.com/account/plugin-domains" target="_blank" rel="noopener">agencypulsewp.com/account/plugin-domains</a>.', 'agency-pulse-admin-menu' ),
				'error',
				true
			);
		}
	}
}
