<?php
/**
 * Debugger
 *
 * Conditional error_log wrapper.
 * Only writes when debug_mode option is on AND WP_DEBUG is true.
 * Zero output in production unless explicitly enabled by the site owner.
 *
 * @package Relatedly\Core
 * @since   2.0.0
 */

namespace Relatedly\Core;

defined( 'ABSPATH' ) || exit;

/**
 * Writes plugin debug messages when debugging is explicitly enabled.
 *
 * @since 2.0.0
 */
class Debugger {

	/**
	 * Plugin settings repository.
	 *
	 * @since 2.0.0
	 * @var Options
	 */
	private Options $options;

	/**
	 * Create the debugger.
	 *
	 * @since 2.0.0
	 * @param Options $options Plugin settings repository.
	 */
	public function __construct( Options $options ) {
		$this->options = $options;
	}

	/**
	 * Log a message with optional context data.
	 *
	 * @param string               $message Human-readable message.
	 * @param array<string, mixed> $context Optional key-value context.
	 */
	public function log( string $message, array $context = [] ): void {
		if ( ! $this->options->get( 'debug_mode', false ) ) {
			return;
		}
		if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
			return;
		}

		$entry = '[Relatedly] ' . $message;
		if ( ! empty( $context ) ) {
			$entry .= ' | ' . wp_json_encode( $context );
		}

		// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
		error_log( $entry );
	}
}
