<?php
/**
 * WooNooW Installation Checker
 * 
 * Upload this file to: wp-content/plugins/woonoow/check-installation.php
 * Then visit: https://yoursite.com/wp-content/plugins/woonoow/check-installation.php
 */

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>WooNooW Installation Check</title>
    <style>
        body { font-family: monospace; padding: 20px; background: #1e1e1e; color: #d4d4d4; }
        .pass { color: #4ec9b0; }
        .fail { color: #f48771; }
        .warn { color: #dcdcaa; }
        h1 { color: #569cd6; }
        h2 { color: #4ec9b0; margin-top: 30px; }
        pre { background: #2d2d2d; padding: 10px; border-radius: 4px; overflow-x: auto; }
        .status { display: inline-block; width: 60px; font-weight: bold; }
    </style>
</head>
<body>
    <h1>WooNooW Installation Checker</h1>
    
    <h2>1. File Structure</h2>
    <?php
    $plugin_dir = __DIR__;
    $required_files = [
        'woonoow.php' => 'Main plugin file',
        'includes/Admin/Assets.php' => 'Assets handler',
        'includes/Api/Routes.php' => 'API routes',
        'admin-spa/dist/app.js' => 'SPA JavaScript',
        'admin-spa/dist/app.css' => 'SPA Stylesheet',
    ];
    
    foreach ($required_files as $file => $desc) {
        $path = $plugin_dir . '/' . $file;
        $exists = file_exists($path);
        $size = $exists ? filesize($path) : 0;
        $status = $exists ? '<span class="pass">✓ PASS</span>' : '<span class="fail">✗ FAIL</span>';
        
        echo "<div>$status - $desc</div>";
        echo "<pre>  Path: $file\n";
        if ($exists) {
            echo "  Size: " . number_format($size) . " bytes\n";
            echo "  Modified: " . date('Y-m-d H:i:s', filemtime($path)) . "</pre>";
        } else {
            echo "  <span class='fail'>FILE NOT FOUND!</span></pre>";
        }
    }
    ?>
    
    <h2>2. PHP Configuration</h2>
    <?php
    $php_checks = [
        'PHP Version' => phpversion(),
        'OPcache Enabled' => function_exists('opcache_get_status') && opcache_get_status() ? 'Yes' : 'No',
        'Memory Limit' => ini_get('memory_limit'),
        'Max Execution Time' => ini_get('max_execution_time') . 's',
        'Upload Max Filesize' => ini_get('upload_max_filesize'),
    ];
    
    foreach ($php_checks as $check => $value) {
        echo "<div><span class='status'>INFO</span> $check: <strong>$value</strong></div>";
    }
    ?>
    
    <h2>3. WordPress Detection</h2>
    <?php
    $wp_load = dirname(dirname(dirname(dirname(__DIR__)))) . '/wp-load.php';
    if (file_exists($wp_load)) {
        require_once $wp_load;
        echo "<div><span class='pass'>✓ PASS</span> WordPress loaded</div>";
        echo "<pre>  WP Version: " . get_bloginfo('version') . "\n";
        echo "  Site URL: " . home_url() . "\n";
        echo "  Admin URL: " . admin_url() . "</pre>";
        
        // Check if plugin is active
        if (function_exists('is_plugin_active')) {
            $is_active = is_plugin_active('woonoow/woonoow.php');
            $status = $is_active ? '<span class="pass">✓ ACTIVE</span>' : '<span class="warn">⚠ INACTIVE</span>';
            echo "<div>$status - Plugin Status</div>";
        }
    } else {
        echo "<div><span class='fail'>✗ FAIL</span> WordPress not found</div>";
    }
    ?>
    
    <h2>4. Constants Check</h2>
    <?php
    if (defined('WOONOOW_ADMIN_DEV')) {
        $dev_mode = WOONOOW_ADMIN_DEV;
        $status = $dev_mode ? '<span class="warn">⚠ WARN</span>' : '<span class="pass">✓ PASS</span>';
        echo "<div>$status - WOONOOW_ADMIN_DEV = " . ($dev_mode ? 'true (DEV MODE!)' : 'false') . "</div>";
    } else {
        echo "<div><span class='pass'>✓ PASS</span> - WOONOOW_ADMIN_DEV not defined (production mode)</div>";
    }
    
    if (defined('WP_DEBUG')) {
        $debug = WP_DEBUG;
        $status = $debug ? '<span class="warn">⚠ WARN</span>' : '<span class="pass">✓ PASS</span>';
        echo "<div>$status - WP_DEBUG = " . ($debug ? 'true' : 'false') . "</div>";
    }
    ?>
    
    <h2>5. Namespace Check (Routes.php)</h2>
    <?php
    $routes_file = $plugin_dir . '/includes/Api/Routes.php';
    if (file_exists($routes_file)) {
        $content = file_get_contents($routes_file);
        $has_correct_namespace = strpos($content, 'use WooNooW\\Api\\PaymentsController') !== false;
        $has_wrong_namespace = strpos($content, 'use WooNooW\\API\\PaymentsController') !== false;
        
        if ($has_correct_namespace) {
            echo "<div><span class='pass'>✓ PASS</span> - Correct namespace (WooNooW\\Api\\)</div>";
        } elseif ($has_wrong_namespace) {
            echo "<div><span class='fail'>✗ FAIL</span> - Wrong namespace (WooNooW\\API\\) - needs update!</div>";
        } else {
            echo "<div><span class='warn'>⚠ WARN</span> - Could not detect namespace</div>";
        }
    }
    ?>
    
    <h2>5b. Dev Mode Filter Check (woonoow.php)</h2>
    <?php
    $main_file = $plugin_dir . '/woonoow.php';
    if (file_exists($main_file)) {
        $content = file_get_contents($main_file);
        $has_dev_filter = strpos($content, "add_filter('woonoow/admin_is_dev', '__return_true')") !== false;
        
        if ($has_dev_filter) {
            echo "<div><span class='fail'>✗ FAIL</span> - Hardcoded dev mode filter found in woonoow.php!</div>";
            echo "<pre>  This forces dev mode ON for everyone.\n";
            echo "  Remove this line from woonoow.php:\n";
            echo "  add_filter('woonoow/admin_is_dev', '__return_true');</pre>";
        } else {
            echo "<div><span class='pass'>✓ PASS</span> - No hardcoded dev mode filters</div>";
        }
    }
    ?>
    
    <h2>6. Asset URLs</h2>
    <?php
    if (function_exists('plugins_url')) {
        $base_url = plugins_url('', $plugin_dir . '/woonoow.php');
        $css_url = $base_url . '/admin-spa/dist/app.css';
        $js_url = $base_url . '/admin-spa/dist/app.js';
        
        echo "<pre>CSS URL: $css_url\n";
        echo "JS URL:  $js_url</pre>";
        
        // Test if URLs are accessible
        echo "<div>Testing URL accessibility...</div>";
        $ch = curl_init($css_url);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_exec($ch);
        $css_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        $status = ($css_code == 200) ? '<span class="pass">✓ PASS</span>' : '<span class="fail">✗ FAIL</span>';
        echo "<div>$status - app.css (HTTP $css_code)</div>";
        
        $ch = curl_init($js_url);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_exec($ch);
        $js_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        $status = ($js_code == 200) ? '<span class="pass">✓ PASS</span>' : '<span class="fail">✗ FAIL</span>';
        echo "<div>$status - app.js (HTTP $js_code)</div>";
    }
    ?>
    
    <h2>7. Recommendations</h2>
    <?php
    $issues = [];
    
    if (!file_exists($plugin_dir . '/admin-spa/dist/app.js')) {
        $issues[] = "❌ Missing admin-spa/dist/app.js - Re-extract the zip file";
    }
    
    if (defined('WOONOOW_ADMIN_DEV') && WOONOOW_ADMIN_DEV) {
        $issues[] = "⚠️ Dev mode is enabled - Remove WOONOOW_ADMIN_DEV from wp-config.php";
    }
    
    if (isset($has_wrong_namespace) && $has_wrong_namespace) {
        $issues[] = "❌ Wrong namespace in Routes.php - Update to latest code";
    }
    
    if (isset($css_code) && $css_code != 200) {
        $issues[] = "❌ CSS file not accessible - Check file permissions";
    }
    
    if (isset($js_code) && $js_code != 200) {
        $issues[] = "❌ JS file not accessible - Check file permissions";
    }
    
    if (empty($issues)) {
        echo "<div><span class='pass'>✓ All checks passed!</span></div>";
        echo "<div>If SPA still not loading, try:</div>";
        echo "<pre>1. Clear browser cache (Ctrl+Shift+R)\n";
        echo "2. Clear OPcache: php -r \"opcache_reset();\"\n";
        echo "3. Clear WordPress cache: wp cache flush</pre>";
    } else {
        echo "<div><span class='fail'>Issues found:</span></div>";
        echo "<ul>";
        foreach ($issues as $issue) {
            echo "<li>$issue</li>";
        }
        echo "</ul>";
    }
    ?>
    
    <h2>8. Quick Actions</h2>
    <div>
        <a href="?action=clear_opcache" style="color: #4ec9b0;">Clear OPcache</a> | 
        <a href="?action=phpinfo" style="color: #4ec9b0;">View PHP Info</a>
    </div>
    
    <?php
    if (isset($_GET['action'])) {
        if ($_GET['action'] == 'clear_opcache' && function_exists('opcache_reset')) {
            opcache_reset();
            echo "<div style='margin-top: 20px;'><span class='pass'>✓ OPcache cleared!</span></div>";
        } elseif ($_GET['action'] == 'phpinfo') {
            echo "<div style='margin-top: 20px;'>";
            phpinfo();
            echo "</div>";
        }
    }
    ?>
    
    <div style="margin-top: 40px; padding-top: 20px; border-top: 1px solid #444; color: #888;">
        Generated: <?php echo date('Y-m-d H:i:s'); ?> | 
        Server: <?php echo $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown'; ?>
    </div>
</body>
</html>
