<?php
/**
 * Test Theme Independence
 * 
 * Verifies that the HVAC plugin works without any specific theme
 * 
 * Usage: wp eval-file test-theme-independence.php
 */

// Check if running in WordPress context
if (!defined('ABSPATH')) {
    die('This script must be run within WordPress context using wp-cli');
}

echo "Testing HVAC Plugin Theme Independence...\n";
echo "=========================================\n\n";

// Test 1: Check for theme-specific constants
echo "Test 1: Checking for theme-specific constants...\n";
$theme_constants = ['ASTRA_THEME_VERSION', 'DIVI_VERSION', 'ET_BUILDER_VERSION'];
$found_constants = [];

foreach ($theme_constants as $constant) {
    if (defined($constant)) {
        $found_constants[] = $constant;
    }
}

if (empty($found_constants)) {
    echo "✓ No theme-specific constants found in environment\n";
} else {
    echo "✗ Found theme constants: " . implode(', ', $found_constants) . "\n";
}

// Test 2: Check for theme-specific classes
echo "\nTest 2: Checking for theme-specific classes...\n";
$plugin_dir = plugin_dir_path(__FILE__);
$theme_classes = [];

// Check if Astra integration class exists
if (file_exists($plugin_dir . 'includes/class-hvac-astra-integration.php')) {
    $theme_classes[] = 'class-hvac-astra-integration.php';
}

// Check if any Divi integration exists
$includes_files = glob($plugin_dir . 'includes/*.php');
foreach ($includes_files as $file) {
    $content = file_get_contents($file);
    if (stripos($content, 'divi') !== false || stripos($content, 'et_builder') !== false) {
        $theme_classes[] = basename($file);
    }
}

if (empty($theme_classes)) {
    echo "✓ No theme-specific classes found\n";
} else {
    echo "✗ Found theme-specific files: " . implode(', ', $theme_classes) . "\n";
}

// Test 3: Check for theme-specific hooks
echo "\nTest 3: Checking for theme-specific hooks...\n";
$theme_hooks = [];
$all_filters = $wp_filter;
$theme_prefixes = ['astra_', 'ast_', 'divi_', 'et_'];

foreach ($all_filters as $hook_name => $callbacks) {
    foreach ($theme_prefixes as $prefix) {
        if (strpos($hook_name, $prefix) === 0) {
            // Check if any HVAC classes are hooked to this
            foreach ($callbacks as $priority => $functions) {
                foreach ($functions as $function) {
                    if (is_array($function['function']) && 
                        is_object($function['function'][0]) && 
                        strpos(get_class($function['function'][0]), 'HVAC') !== false) {
                        $theme_hooks[] = $hook_name;
                        break 2;
                    }
                }
            }
        }
    }
}

if (empty($theme_hooks)) {
    echo "✓ No HVAC hooks attached to theme-specific filters\n";
} else {
    echo "✗ Found HVAC hooks on theme filters: " . implode(', ', array_unique($theme_hooks)) . "\n";
}

// Test 4: Check Layout Manager
echo "\nTest 4: Checking Layout Manager...\n";
if (class_exists('HVAC_Layout_Manager')) {
    echo "✓ Theme-agnostic Layout Manager is loaded\n";
    
    // Check if it's working
    $layout_manager = HVAC_Layout_Manager::instance();
    if (is_object($layout_manager)) {
        echo "✓ Layout Manager instance created successfully\n";
    }
} else {
    echo "✗ Layout Manager class not found\n";
}

// Test 5: Check CSS files
echo "\nTest 5: Checking for theme-specific CSS files...\n";
$css_dir = $plugin_dir . 'assets/css/';
$theme_css_files = [];

if (is_dir($css_dir)) {
    $css_files = glob($css_dir . '*.css');
    foreach ($css_files as $file) {
        $filename = basename($file);
        if (stripos($filename, 'astra') !== false || 
            stripos($filename, 'ast-') !== false || 
            stripos($filename, 'divi') !== false) {
            $theme_css_files[] = $filename;
        }
    }
}

if (empty($theme_css_files)) {
    echo "✓ No theme-specific CSS files found\n";
} else {
    echo "✗ Found theme CSS files: " . implode(', ', $theme_css_files) . "\n";
}

// Test 6: Check JS files
echo "\nTest 6: Checking for theme-specific JS files...\n";
$js_dir = $plugin_dir . 'assets/js/';
$theme_js_files = [];

if (is_dir($js_dir)) {
    $js_files = glob($js_dir . '*.js');
    foreach ($js_files as $file) {
        $filename = basename($file);
        if (stripos($filename, 'astra') !== false || 
            stripos($filename, 'ast-') !== false || 
            stripos($filename, 'divi') !== false) {
            $theme_js_files[] = $filename;
        }
    }
}

if (empty($theme_js_files)) {
    echo "✓ No theme-specific JS files found\n";
} else {
    echo "✗ Found theme JS files: " . implode(', ', $theme_js_files) . "\n";
}

// Summary
echo "\n=========================================\n";
echo "THEME INDEPENDENCE TEST SUMMARY\n";
echo "=========================================\n";

$total_issues = count($found_constants) + count($theme_classes) + count($theme_hooks) + 
                count($theme_css_files) + count($theme_js_files);

if ($total_issues === 0) {
    echo "✓ PASSED: Plugin is theme-independent!\n";
    echo "The HVAC plugin can work with any WordPress theme.\n";
} else {
    echo "✗ FAILED: Found $total_issues theme-specific issues\n";
    echo "The plugin still has dependencies on specific themes.\n";
}

echo "\n";