<?php
/**
 * Minimal logger.
 *
 * @package WPDudeProducts
 */

declare(strict_types=1);

namespace WPDude\Products\Logging;

/**
 * Lightweight logger (error_log backed).
 */
final class Logger {

	/**
	 * @var self|null
	 */
	private static ?self $instance = null;

	public static function instance(): self {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * @param array<string, mixed> $context Context.
	 */
	public function info( string $message, array $context = array() ): void {
		$this->write( 'info', $message, $context );
	}

	/**
	 * @param array<string, mixed> $context Context.
	 */
	public function warning( string $message, array $context = array() ): void {
		$this->write( 'warning', $message, $context );
	}

	/**
	 * @param array<string, mixed> $context Context.
	 */
	public function error( string $message, array $context = array() ): void {
		$this->write( 'error', $message, $context );
	}

	/**
	 * @param array<string, mixed> $context Context.
	 */
	private function write( string $level, string $message, array $context ): void {
		$suffix = $context ? ' ' . wp_json_encode( $context ) : '';
		// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
		error_log( '[Dude Commerce][' . $level . '] ' . $message . $suffix );
	}
}
