<?php
/**
 * Logger utility.
 *
 * @package MasterQuiz
 * @since   1.0.0
 */

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

/**
 * Class MasterQuiz_Logger
 *
 * Simple logging utility that writes to WP_DEBUG_LOG when enabled.
 *
 * @since 1.0.0
 */
class MasterQuiz_Logger {

	/**
	 * Log a message.
	 *
	 * @since 1.0.0
	 *
	 * @param string $message The log message.
	 * @param string $level   Log level: info, warning, error.
	 * @param array  $context Optional additional context data.
	 */
	public static function log( $message, $level = 'info', $context = array() ) {
		if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
			return;
		}

		$formatted = sprintf(
			'[MasterQuiz AI][%s] %s',
			strtoupper( $level ),
			$message
		);

		if ( ! empty( $context ) ) {
			$formatted .= ' ' . wp_json_encode( $context );
		}

		if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
			// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
			error_log( $formatted );
		}
	}

	/**
	 * Log an info message.
	 *
	 * @since 1.0.0
	 *
	 * @param string $message The log message.
	 * @param array  $context Optional context data.
	 */
	public static function info( $message, $context = array() ) {
		self::log( $message, 'info', $context );
	}

	/**
	 * Log a warning message.
	 *
	 * @since 1.0.0
	 *
	 * @param string $message The log message.
	 * @param array  $context Optional context data.
	 */
	public static function warning( $message, $context = array() ) {
		self::log( $message, 'warning', $context );
	}

	/**
	 * Log an error message.
	 *
	 * @since 1.0.0
	 *
	 * @param string $message The log message.
	 * @param array  $context Optional context data.
	 */
	public static function error( $message, $context = array() ) {
		self::log( $message, 'error', $context );
	}
}
