<?php
/**
 * Tests for HVAC_Logger class
 *
 * @package HVAC_Community_Events
 * @subpackage Tests
 */

/**
 * Class HVAC_Logger_Test
 */
class HVAC_Logger_Test extends WP_UnitTestCase {

	/**
	 * Test logger initialization
	 */
	public function test_logger_initialization() {
		// Reset logger state
		$reflection = new ReflectionClass( 'HVAC_Logger' );
		$enabled_property = $reflection->getProperty( 'enabled' );
		$enabled_property->setAccessible( true );
		$enabled_property->setValue( null, null );

		// Test initialization
		HVAC_Logger::init();
		
		// Logger should be disabled by default in test environment
		$this->assertNotNull( $enabled_property->getValue() );
	}

	/**
	 * Test logging when disabled
	 */
	public function test_logging_when_disabled() {
		HVAC_Logger::set_enabled( false );
		
		// This should not produce any output
		$this->expectOutputString( '' );
		HVAC_Logger::log( 'Test message' );
	}

	/**
	 * Test logging when enabled
	 */
	public function test_logging_when_enabled() {
		// Enable logging
		HVAC_Logger::set_enabled( true );
		
		// Capture error log output
		$this->setOutputCallback( function( $output ) {
			return ''; // Suppress output for tests
		} );
		
		// Log a message
		HVAC_Logger::log( 'Test message', 'TestContext' );
		
		// We can't easily test error_log output, but we can verify the method executes
		$this->assertTrue( true );
	}

	/**
	 * Test different log levels
	 */
	public function test_log_levels() {
		HVAC_Logger::set_enabled( true );
		
		// Test error logging
		HVAC_Logger::error( 'Test error', 'TestContext' );
		
		// Test warning logging
		HVAC_Logger::warning( 'Test warning', 'TestContext' );
		
		// Test info logging
		HVAC_Logger::info( 'Test info', 'TestContext' );
		
		// If no exceptions thrown, test passes
		$this->assertTrue( true );
	}

	/**
	 * Test logging with data
	 */
	public function test_logging_with_data() {
		HVAC_Logger::set_enabled( true );
		
		$test_data = array(
			'user_id' => 123,
			'action' => 'test_action',
		);
		
		HVAC_Logger::log( 'Test with data', 'TestContext', $test_data );
		
		// If no exceptions thrown, test passes
		$this->assertTrue( true );
	}

	/**
	 * Test debug nonce generation
	 */
	public function test_debug_nonce() {
		$nonce = HVAC_Logger::get_debug_nonce();
		
		$this->assertNotEmpty( $nonce );
		$this->assertIsString( $nonce );
		
		// Verify nonce is valid
		$this->assertNotFalse( wp_verify_nonce( $nonce, 'hvac_debug_nonce' ) );
	}

	/**
	 * Test enable/disable persistence
	 */
	public function test_enable_disable_persistence() {
		// Enable logging
		HVAC_Logger::set_enabled( true );
		$option_value = get_option( 'hvac_ce_debug_mode' );
		$this->assertTrue( $option_value );
		
		// Disable logging
		HVAC_Logger::set_enabled( false );
		$option_value = get_option( 'hvac_ce_debug_mode' );
		$this->assertFalse( $option_value );
	}
}