<?php

namespace BricksExtras;

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

/**
 * Toast Queue Helper
 * 
 * Allows developers to queue toast notifications from PHP that will be displayed on page load.
 * 
 * @since 1.0.0
 */
class BricksExtrasToast {

	/**
	 * Session key for storing queued toasts
	 */
	const SESSION_KEY = 'bricksextras_toast_queue';

	/**
	 * Initialize the toast queue system
	 */
	public static function init() {
		// Start session if not already started
		add_action( 'init', [ __CLASS__, 'maybe_start_session' ], 1 );
		
		// Enqueue toasts in footer
		add_action( 'wp_footer', [ __CLASS__, 'render_queued_toasts' ], 999 );
	}

	/**
	 * Start session if not already started
	 */
	public static function maybe_start_session() {

		if ( ! session_id() && ! headers_sent() ) {
			session_start();
		}
	}

	/**
	 * Get session data.
	 *
	 * @return array Validated session queue data.
	 */
	private static function get_session_queue() {

		$session_data = $_SESSION[ self::SESSION_KEY ] ?? [];

		if ( ! is_array( $session_data ) ) {
			return [];
		}

		$validated = [];
		foreach ( $session_data as $item ) {
			
			if ( ! is_array( $item ) ) {
				continue;
			}

			if ( isset( $item['notification_id'], $item['message'], $item['announce'], $item['duration'], $item['context_icon'], $item['dismissible'], $item['close_button'], $item['timestamp'] ) &&
			     is_string( $item['notification_id'] ) &&
			     is_string( $item['message'] ) &&
			     is_string( $item['announce'] ) &&
			     in_array( $item['announce'], [ 'polite', 'assertive', 'off' ], true ) &&
			     is_int( $item['duration'] ) &&
			     is_bool( $item['context_icon'] ) &&
			     is_bool( $item['dismissible'] ) &&
			     is_bool( $item['close_button'] ) &&
			     is_int( $item['timestamp'] ) ) {
				$validated[] = $item;
			}
		}

		return $validated;
	}

	/**
	 * Queue a toast notification
	 * 
	 * @param string $notification_id The Bricks element ID of the toast notification (e.g., 'ghijkl')
	 * @param string $message Optional message to override notification content
	 * @param string $announce ARIA live region announcement: 'polite', 'assertive', or 'off' (default: 'polite')
	 * @param int $duration Duration in milliseconds (default: 5000)
	 * @param bool $context_icon Include context icon if it exists (default: true)
	 * @param bool $dismissible Allow swipe to dismiss (default: true)
	 * @param bool $close_button Show close button (default: false)
	 * 
	 */
	public static function add( $notification_id, $message = '', $announce = 'polite', $duration = 5000, $context_icon = true, $dismissible = true, $close_button = false ) {

		// Validate announce value.
		$allowed_announce = [ 'polite', 'assertive', 'off' ];
		if ( ! in_array( $announce, $allowed_announce, true ) ) {
			$announce = 'polite';
		}

		$toast_data = [
			'notification_id' => sanitize_text_field( $notification_id ),
			'message' => $message ? wp_kses_post( $message ) : '',
			'announce' => sanitize_text_field( $announce ),
			'duration' => intval( $duration ),
			'context_icon' => (bool) $context_icon,
			'dismissible' => (bool) $dismissible,
			'close_button' => (bool) $close_button,
			'timestamp' => time(),
		];

		// Check for duplicate - prevent same notification from being queued multiple times.
		$queue = self::get_session_queue();
		foreach ( $queue as $existing ) {
			// Validate existing item structure.
			if ( ! is_array( $existing ) || 
			     ! isset( $existing['notification_id'], $existing['message'], $existing['timestamp'] ) ) {
				continue;
			}

			if ( $existing['notification_id'] === $toast_data['notification_id'] && 
			     $existing['message'] === $toast_data['message'] &&
			     ( time() - $existing['timestamp'] ) < 2 ) { // Within 2 seconds.
				return;
			}
		}

		// Add to session.
		if ( ! isset( $_SESSION[ self::SESSION_KEY ] ) ) {
			$_SESSION[ self::SESSION_KEY ] = [];
		}
		$_SESSION[ self::SESSION_KEY ][] = $toast_data;
	}


	/**
	 * Get all queued toasts.
	 *
	 * @return array
	 */
	public static function get_queue() {
		$queue = self::get_session_queue();

		$validated_queue = [];
		foreach ( $queue as $item ) {
			if ( is_array( $item ) && isset( $item['notification_id'] ) ) {
				$validated_queue[] = $item;
			}
		}

		return $validated_queue;
	}

	/**
	 * Clear the toast queue.
	 */
	public static function clear() {
		if ( isset( $_SESSION[ self::SESSION_KEY ] ) ) {
			unset( $_SESSION[ self::SESSION_KEY ] );
		}
	}


	/**
	 * Render queued toasts as data attribute.
	 */
	public static function render_queued_toasts() {
		$queue = self::get_queue();

		if ( empty( $queue ) ) {
			return;
		}

		// Clear the queue after retrieving.
		self::clear();
		?>
		
		<script id="bricksextras-toast-queue" type="application/json">
		<?php echo wp_json_encode( $queue, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); ?>
		</script>
		<?php
	}
}