<?php
/**
 * LoggerService class.
 *
 * A wrapper around WooCommerce logger that adds Firevert-specific context and formatting.
 *
 * @package Ilabs\Firevert\Core\Services
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Services;

use WC_Logger;
use WC_Log_Levels;

/**
 * Wrapper around WooCommerce logger with Firevert-specific context.
 */
final class LoggerService {

	/**
	 * WooCommerce logger instance.
	 *
	 * @var WC_Logger
	 */
	private WC_Logger $logger;

	/**
	 * Logging context (used as source in Woo logs).
	 *
	 * @var string
	 */
	private string $context;

	/**
	 * Constructor.
	 *
	 * @param WC_Logger|null $logger WooCommerce logger instance (DI or fallback).
	 * @param string         $context Optional context slug for log source.
	 */
	public function __construct( WC_Logger $logger = null, string $context = 'firevert' ) {
		$this->logger  = $logger ?? wc_get_logger();
		$this->context = $context;
	}

	/**
	 * Logs debug message.
	 *
	 * @param mixed $message Message to log.
	 *
	 * @return void
	 */
	public function debug( $message ): void {
		$this->logger->log( WC_Log_Levels::DEBUG, $this->format( $message ), array( 'source' => $this->context ) );
	}

	/**
	 * Logs informational message.
	 *
	 * @param mixed $message Message to log.
	 *
	 * @return void
	 */
	public function info( $message ): void {
		$this->logger->log( WC_Log_Levels::INFO, $this->format( $message ), array( 'source' => $this->context ) );
	}

	/**
	 * Logs error message.
	 *
	 * @param mixed $message Message to log.
	 *
	 * @return void
	 */
	public function error( $message ): void {
		$this->logger->log( WC_Log_Levels::ERROR, $this->format( $message ), array( 'source' => $this->context ) );
	}

	/**
	 * Formats message for logging.
	 *
	 * @param mixed $message Message content.
	 *
	 * @return string Formatted message.
	 */
	private function format( $message ): string {
		if ( is_array( $message ) || is_object( $message ) ) {
			// phpcs:disable
			return print_r( $message, true );
		}

		return (string) $message;
	}
}
