<?php
/**
 * Logger helper functions.
 *
 * @package Awesomemotive\Rewardswp\Helpers
 * @since 1.1.0
 */

namespace Awesomemotive\Rewardswp\Helpers;

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

use Awesomemotive\Rewardswp\Interfaces\LoggerInterface;
use Awesomemotive\Rewardswp\Services\LoggerService;

/**
 * Retrieve the logger instance from the container.
 *
 * Falls back to a standalone LoggerService when the container is not available.
 *
 * @since 1.1.0
 *
 * @return LoggerInterface
 */
function rewardswp_logger(): LoggerInterface {
	static $logger = null;

	if ( $logger instanceof LoggerInterface ) {
		return $logger;
	}

	if ( function_exists( 'am_rewardswp' ) ) {
		try {
			$container = am_rewardswp()->get_container();
			$resolved  = $container->get( LoggerInterface::class );

			if ( $resolved instanceof LoggerInterface ) {
				$logger = $resolved;
				return $logger;
			}
		} catch ( \Throwable $throwable ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
			// Fall through to local logger instance.
		}
	}

	$logger = new LoggerService();

	return $logger;
}

/**
 * Convenience wrapper to log an error level message.
 *
 * @since 1.1.0
 *
 * @param string $message Log message.
 * @param array  $context Optional context.
 */
function rewardswp_log_error( string $message, array $context = [] ): void {
	rewardswp_logger()->error( $message, $context );
}

/**
 * Convenience wrapper to log a warning level message.
 *
 * @since 1.1.0
 *
 * @param string $message Log message.
 * @param array  $context Optional context.
 */
function rewardswp_log_warning( string $message, array $context = [] ): void {
	rewardswp_logger()->warning( $message, $context );
}

/**
 * Convenience wrapper to log informational messages.
 *
 * @since 1.1.0
 *
 * @param string $message Log message.
 * @param array  $context Optional context.
 */
function rewardswp_log_info( string $message, array $context = [] ): void {
	rewardswp_logger()->info( $message, $context );
}

/**
 * Convenience wrapper to log debug level messages.
 *
 * @since 1.1.0
 *
 * @param string $message Log message.
 * @param array  $context Optional context.
 */
function rewardswp_log_debug( string $message, array $context = [] ): void {
	rewardswp_logger()->debug( $message, $context );
}


