<?php
/**
 * Logger Class
 *
 * @package FunnelKit_Square
 */

namespace FKWCSQ\Includes;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Logger Class
 */
class Logger {
	/**
	 * Log source identifier
	 *
	 * @var string
	 */
	private static $source = 'fkwcsq-square';

	/**
	 * General settings cache
	 *
	 * @var array|null
	 */
	private static $general_settings = null;

	/**
	 * Initialize logger
	 */
	public static function init() {
		// Load settings on initialization
		self::load_settings();
	}

	/**
	 * Load general settings
	 */
	private static function load_settings() {
		if ( self::$general_settings === null ) {
			self::$general_settings = get_option( 'fkwcsq_settings', array() );
		}
	}

	/**
	 * Check if logging is enabled
	 *
	 * @return bool
	 */
	private static function is_logging_enabled() {
		self::load_settings();
		return isset( self::$general_settings['enable_logging'] ) && self::$general_settings['enable_logging'] === 'yes';
	}

	/**
	 * Format JSON data with proper flags for better readability
	 *
	 * @param mixed $data Data to format as JSON
	 * @return string Formatted JSON string
	 */
	public static function format_json( $data ) {
		return wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT );
	}

	/**
	 * Log debug message
	 *
	 * @param string $message Message to log
	 */
	public static function debug( $message ) {
		if ( ! self::is_logging_enabled() ) {
			return;
		}
		self::log( $message, 'debug' );
	}

	/**
	 * Log error message
	 *
	 * @param string $message Message to log
	 */
	public static function error( $message ) {
		if ( ! self::is_logging_enabled() ) {
			return;
		}
		self::log( $message, 'error' );
	}


	/**
	 * Log info message
	 *
	 * @param string $message Message to log
	 */
	public static function info( $message ) {
		if ( ! self::is_logging_enabled() ) {
			return;
		}
		self::log( $message, 'info' );
	}

	/**
	 * Log message
	 *
	 * @param string $message Message to log
	 * @param string $level Log level
	 */
	public static function log( $message, $level = 'debug' ) {
		if ( ! self::is_logging_enabled() ) {
			return;
		}

		if ( strpos( $message, '{"' ) !== false || strpos( $message, '[' ) !== false ) {
			$message = preg_replace_callback(
				'/(\{.*?\}|\[.*?\])/s',
				function ( $matches ) {
					$json_string = $matches[1];
					$decoded     = json_decode( $json_string, true );
					if ( $decoded !== null ) {
						return self::format_json( $decoded );
					}
					return $json_string;
				},
				$message
			);
		}

		// Format log message
		$log_message = sprintf( '[%s] [%s] %s', \WC_Geolocation::get_ip_address(), strtoupper( $level ), $message );

		// Use WooCommerce logger
		if ( function_exists( 'wc_get_logger' ) ) {
			$logger = wc_get_logger();
			$logger->log( $level, $log_message, array( 'source' => self::$source ) );
		} else {
			// Fallback to error_log if WooCommerce logger is not available
			error_log( '[' . self::$source . '] ' . $log_message );
		}
	}

	/**
	 * Log a warning message
	 *
	 * @param string $message Warning message to log
	 */
	public static function warning( $message ) {
		if ( ! self::is_logging_enabled() ) {
			return;
		}
		self::log( $message, 'warning' );
	}
}
