<?php
/**
 * HJCyberX Checkout for Paddle Lite Logger Utility.
 *
 * Wraps WC_Logger so every log entry carries the "paddlekonnect"
 * source handle. Logs are viewable at:
 * WooCommerce → Status → Logs → paddlekonnect-*.log
 *
 * @package HJCyberX Checkout for Paddle Lite
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class PaddleKonnect_Lite_Logger
 *
 * Static facade over WC_Logger.
 */
class PaddleKonnect_Lite_Logger {

	/**
	 * The WC_Logger instance.
	 *
	 * @var WC_Logger|null
	 */
	private static ?WC_Logger $logger = null;

	/**
	 * Log source handle (appears in filenames and DB entries).
	 *
	 * @var string
	 */
	private static string $source = 'hjcyberx-checkout-for-paddle-lite';

	// ──────────────────────────────────────────.
	// Public API.
	// ──────────────────────────────────────────.

	/**
	 * Log an informational message.
	 *
	 * @param string $message Human-readable message.
	 * @param array  $context Optional associative context data.
	 */
	public static function info( string $message, array $context = array() ): void {
		self::log( 'info', $message, $context );
	}

	/**
	 * Log an error message.
	 *
	 * @param string $message Human-readable message.
	 * @param array  $context Optional associative context data.
	 */
	public static function error( string $message, array $context = array() ): void {
		self::log( 'error', $message, $context );
	}

	/**
	 * Log a debug-level message. Only useful when
	 * WC_LOG_THRESHOLD is set to DEBUG.
	 *
	 * @param string $message Human-readable message.
	 * @param array  $context Optional associative context data.
	 */
	public static function debug( string $message, array $context = array() ): void {
		self::log( 'debug', $message, $context );
	}

	/**
	 * Log a warning-level message.
	 *
	 * @param string $message Human-readable message.
	 * @param array  $context Optional associative context data.
	 */
	public static function warning( string $message, array $context = array() ): void {
		self::log( 'warning', $message, $context );
	}

	// ──────────────────────────────────────────.
	// Internals.
	// ──────────────────────────────────────────.

	/**
	 * Write a log entry.
	 *
	 * @param string $level   WC log level (emergency|alert|critical|error|warning|notice|info|debug).
	 * @param string $message Human-readable message.
	 * @param array  $context Optional context.
	 */
	private static function log( string $level, string $message, array $context = array() ): void {

		if ( null === self::$logger ) {
			self::$logger = wc_get_logger();
		}

		$context['source'] = self::$source;

		self::$logger->log( $level, $message, $context );
	}
}
