<?php
/**
 * Webhook Context class file.
 *
 * @package Simplyin\Simplyin_W_Plugin\Payments\Gateway
 */

declare( strict_types=1 );

namespace Simplyin\Simplyin_W_Plugin\Payments\Gateway;

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

/**
 * Tracks order IDs currently being updated from a webhook response.
 *
 * Breaks the feedback loop: webhook → WC order status update →
 * woocommerce_order_status_changed → Simplyin_Sync::change_status()
 * → re-queues the same status update back to Simply.IN API.
 *
 * Single PHP request scope — no DB needed.
 */
final class Webhook_Context {

	/**
	 * Order IDs currently being processed by a webhook.
	 *
	 * @var array<int, bool>
	 */
	private static array $in_flight = [];

	/**
	 * Mark an order as currently being updated from a webhook.
	 *
	 * @param int $order_id WooCommerce order ID.
	 */
	public static function set_processing( int $order_id ): void {
		self::$in_flight[ $order_id ] = true;
	}

	/**
	 * Check whether an order is currently being updated from a webhook.
	 *
	 * @param int $order_id WooCommerce order ID.
	 * @return bool
	 */
	public static function is_processing( int $order_id ): bool {
		return isset( self::$in_flight[ $order_id ] );
	}

	/**
	 * Remove an order from the in-flight registry.
	 *
	 * @param int $order_id WooCommerce order ID.
	 */
	public static function clear( int $order_id ): void {
		unset( self::$in_flight[ $order_id ] );
	}
}
