<?php
/**
 * HVAC Community Events Logger
 *
 * Centralized logging system for the plugin
 *
 * @package    HVAC_Community_Events
 * @subpackage Includes
 * @since      1.1.0
 */

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

/**
 * Class HVAC_Logger
 *
 * Handles all debug logging for the plugin
 */
class HVAC_Logger {

	/**
	 * Whether logging is enabled
	 *
	 * @var bool
	 */
	private static $enabled = null;

	/**
	 * Log prefix for all messages
	 *
	 * @var string
	 */
	private static $prefix = '[HVAC CE]';

	/**
	 * Initialize the logger
	 *
	 * @return void
	 */
	public static function init() {
		if ( null === self::$enabled ) {
			self::$enabled = self::is_logging_enabled();
		}
	}

	/**
	 * Check if logging is enabled
	 *
	 * @return bool
	 */
	private static function is_logging_enabled() {
		// Check for WP_DEBUG constant
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			return true;
		}

		// Check for plugin-specific debug option
		$plugin_debug = get_option( 'hvac_ce_debug_mode', false );
		
		// Check for query parameter (for temporary debugging) - admin only
		if ( isset( $_GET['hvac_debug'] ) && wp_verify_nonce( $_GET['hvac_debug'], 'hvac_debug_nonce' ) && current_user_can( 'manage_options' ) ) {
			return true;
		}

		return (bool) $plugin_debug;
	}

	/**
	 * Log a debug message
	 *
	 * @param string $message The message to log
	 * @param string $context Optional context/category
	 * @param array  $data    Optional data to include
	 * @return void
	 */
	public static function log( $message, $context = '', $data = array() ) {
		self::init();

		if ( ! self::$enabled ) {
			return;
		}

		$log_message = self::$prefix;

		if ( ! empty( $context ) ) {
			$log_message .= " [{$context}]";
		}

		$log_message .= " {$message}";

		if ( ! empty( $data ) ) {
			$log_message .= ' | Data: ' . print_r( $data, true );
		}

		// Use WordPress error_log function
		error_log( $log_message );
	}

	/**
	 * Log an error
	 *
	 * @param string $message The error message
	 * @param string $context Optional context
	 * @param array  $data    Optional error data
	 * @return void
	 */
	public static function error( $message, $context = '', $data = array() ) {
		self::log( "[ERROR] {$message}", $context, $data );
	}

	/**
	 * Log a warning
	 *
	 * @param string $message The warning message
	 * @param string $context Optional context
	 * @param array  $data    Optional warning data
	 * @return void
	 */
	public static function warning( $message, $context = '', $data = array() ) {
		self::log( "[WARNING] {$message}", $context, $data );
	}

	/**
	 * Log an info message
	 *
	 * @param string $message The info message
	 * @param string $context Optional context
	 * @param array  $data    Optional data
	 * @return void
	 */
	public static function info( $message, $context = '', $data = array() ) {
		self::log( "[INFO] {$message}", $context, $data );
	}

	/**
	 * Enable or disable logging
	 *
	 * @param bool $enabled Whether to enable logging
	 * @return void
	 */
	public static function set_enabled( $enabled ) {
		self::$enabled = (bool) $enabled;
		update_option( 'hvac_ce_debug_mode', self::$enabled );
	}

	/**
	 * Get a debug nonce for temporary debugging
	 *
	 * @return string
	 */
	public static function get_debug_nonce() {
		return wp_create_nonce( 'hvac_debug_nonce' );
	}
}