<?php
/**
 * Tests for WordPress hooks and filters integration
 */

// Ensure we're running in test mode
if (!defined('HVAC_TEST_DIR')) {
    require_once dirname(__DIR__) . '/bootstrap-staging.php';
}

class HVAC_Hooks_Test extends PHPUnit\Framework\TestCase {
    
    protected $hook_calls = [];
    protected $filter_values = [];

    protected function setUp(): void {
        parent::setUp();
        // Set staging environment if needed
        if (!getenv('HVAC_ENV')) {
            putenv('HVAC_ENV=staging');
        }
        
        // Reset tracking arrays
        $this->hook_calls = [];
        $this->filter_values = [];
    }

    public function test_plugin_activation_hooks() {
        // Test activation hook registration
        $result = register_activation_hook(
            HVAC_PLUGIN_DIR . '/hvac-community-events.php',
            function() {}
        );
        
        $this->assertTrue(
            $result,
            'Activation hook should be registered successfully'
        );

        // Test deactivation hook registration
        $result = register_deactivation_hook(
            HVAC_PLUGIN_DIR . '/hvac-community-events.php',
            function() {}
        );
        
        $this->assertTrue(
            $result,
            'Deactivation hook should be registered successfully'
        );
    }

    public function test_event_hooks() {
        // Track hook execution
        add_action('hvac_before_event_creation', function($event_data) {
            $this->hook_calls[] = [
                'hook' => 'hvac_before_event_creation',
                'data' => $event_data
            ];
        });

        // Simulate event creation
        do_action('hvac_before_event_creation', [
            'title' => 'Test Event',
            'date' => '2025-04-10'
        ]);

        $this->assertCount(
            1,
            $this->hook_calls,
            'Event creation hook should be called once'
        );
        
        $this->assertEquals(
            'Test Event',
            $this->hook_calls[0]['data']['title'],
            'Event data should be passed to hook correctly'
        );
    }

    public function test_content_filters() {
        // Add test filter
        add_filter('hvac_event_content', function($content) {
            $this->filter_values[] = $content;
            return 'Filtered: ' . $content;
        });

        // Test content filtering
        $original_content = 'Original event content';
        $filtered_content = apply_filters('hvac_event_content', $original_content);

        $this->assertStringContainsString(
            'Filtered:',
            $filtered_content,
            'Content should be modified by filter'
        );
        
        $this->assertCount(
            1,
            $this->filter_values,
            'Filter should be called once'
        );
        
        $this->assertEquals(
            $original_content,
            $this->filter_values[0],
            'Original content should be passed to filter'
        );
    }

    public function test_conditional_hooks() {
        // Test is_page mock
        $this->assertFalse(
            is_page('events'),
            'is_page mock should return false by default'
        );

        // Test is_singular mock
        $this->assertFalse(
            is_singular('tribe_events'),
            'is_singular mock should return false by default'
        );
    }

    public function test_url_functions() {
        $this->assertEquals(
            'http://example.com/events',
            home_url('/events'),
            'home_url should return correct test URL'
        );
    }
}

// Add required function if not exists
if (!function_exists('do_action')) {
    function do_action($hook, ...$args) {
        error_log("[HVAC TEST] Mock do_action called for hook: $hook");
        return true;
    }
}

if (!function_exists('apply_filters')) {
    function apply_filters($hook, $value, ...$args) {
        error_log("[HVAC TEST] Mock apply_filters called for hook: $hook");
        return 'Filtered: ' . $value;
    }
}