<?php
/**
 * Wayl Logger
 *
 * Handles logging for Wayl plugin operations.
 *
 * @package Wayl_Checkout
 * @since 1.2.0
 */

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

/**
 * Class WaylCHP_Logger
 */
class WaylCHP_Logger {
	/**
	 * WC_Logger instance
	 *
	 * @var WC_Logger|null
	 */
	private static $logger;

	/**
	 * Log directory path
	 *
	 * @var string|null
	 */
	private static $log_directory;

	/**
	 * Initialize the logger
	 *
	 * @return void
	 */
	public static function init() {
		// Create logs directory if it doesn't exist
		$upload_dir = wp_upload_dir();
		self::$log_directory = $upload_dir['basedir'] . '/wayl-checkout/';
		if ( ! file_exists( self::$log_directory ) ) {
			wp_mkdir_p( self::$log_directory );
		}
	}

	/**
	 * Log a message to Wayl log files
	 *
	 * @param string $message Log message
	 * @param string $level Log level: 'info', 'error', 'warning', 'debug'
	 * @param bool   $force Force logging even if disabled (for critical operations like refunds)
	 * @return void
	 */
	public static function log( $message, $level = 'info', $force = false ) {
		// Logging is opt-in only (disabled by default)
		// But force logging for critical operations like refunds
		// Check WooCommerce gateway settings for logging option
		$gateway_settings = get_option( 'woocommerce_wayl_settings', array() );
		$enabled = isset( $gateway_settings['logging'] ) && 'yes' === $gateway_settings['logging'];

		if ( ! $enabled && ! $force ) {
			return;
		}

		// Initialize logger if needed
		if ( ! self::$logger ) {
			self::init();
			if ( class_exists( 'WooCommerce' ) ) {
				self::$logger = new WC_Logger();
			} else {
				return; // WooCommerce not available
			}
		}

		// Create timestamp
		$timestamp = current_time( 'Y-m-d H:i:s' );

		// Format log message
		$log_message = sprintf( '[%s] [%s] %s', $timestamp, strtoupper( $level ), $message );

		// Log to WooCommerce logger (for admin interface)
		if ( self::$logger ) {
			self::$logger->log( $log_message, $level, array( 'source' => 'waylchp' ) );
		}

		// Also write to dedicated Wayl log file
		$log_file  = self::$log_directory . 'waylchp-' . gmdate( 'Y-m-d' ) . '.log';
		$log_entry = $log_message . PHP_EOL;

		// Use WP_Filesystem for file operations
		global $wp_filesystem;
		if ( empty( $wp_filesystem ) ) {
			require_once ABSPATH . '/wp-admin/includes/file.php';
			WP_Filesystem();
		}

		if ( $wp_filesystem && $wp_filesystem->is_writable( self::$log_directory ) ) {
			// Get existing content and append new entry
			$existing_content = $wp_filesystem->get_contents( $log_file );
			if ( false === $existing_content ) {
				$existing_content = '';
			}
			$wp_filesystem->put_contents( $log_file, $existing_content . $log_entry, FS_CHMOD_FILE );
		} elseif ( file_exists( $log_file ) && is_writable( $log_file ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable
			// Fallback for direct file access if WP_Filesystem is not available
			file_put_contents( $log_file, $log_entry, FILE_APPEND | LOCK_EX ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
		}
	}

	/**
	 * Log info message
	 *
	 * @param string $message Log message
	 * @param bool   $force Force logging even if disabled
	 * @return void
	 */
	public static function info( $message, $force = false ) {
		self::log( $message, 'info', $force );
	}

	/**
	 * Log error message
	 *
	 * @param string $message Log message
	 * @param bool   $force Force logging even if disabled
	 * @return void
	 */
	public static function error( $message, $force = false ) {
		self::log( $message, 'error', $force );
	}

	/**
	 * Log warning message
	 *
	 * @param string $message Log message
	 * @param bool   $force Force logging even if disabled
	 * @return void
	 */
	public static function warning( $message, $force = false ) {
		self::log( $message, 'warning', $force );
	}

	/**
	 * Log debug message
	 *
	 * @param string $message Log message
	 * @param bool   $force Force logging even if disabled
	 * @return void
	 */
	public static function debug( $message, $force = false ) {
		self::log( $message, 'debug', $force );
	}

	/**
	 * Get log directory path
	 *
	 * @return string Log directory path
	 */
	public static function get_log_directory() {
		if ( ! self::$log_directory ) {
			self::init();
		}
		return self::$log_directory;
	}
}

