<?php
// Test if admin_seo_manager.php has any syntax errors by checking it with curl
$url = 'http://localhost/ecoms/admin_seo_manager.php?action=seo-products';
$response = @file_get_contents($url);

if ($response === false) {
    die("Failed to load page");
}

// Check for PHP errors
$php_errors = array();
if (strpos($response, 'Parse error') !== false || 
    strpos($response, 'Syntax error') !== false ||
    strpos($response, 'Fatal error') !== false) {
    echo "PHP Errors found!";
    echo htmlspecialchars(substr($response, 0, 2000));
    die;
}

// Extract script section
preg_match('/<script>(.*?)<\/script>/s', $response, $matches);
if (isset($matches[1])) {
    $script = $matches[1];
    
    // Check for key functions
    $functions = array('loadProducts', 'loadCategories', 'loadBlogs', 'editCategorySEO', 'editProductSEO', 'editBlogSEO');
    
    echo "<h2>JavaScript Functions Check</h2>";
    foreach ($functions as $func) {
        if (strpos($script, "function $func(") !== false) {
            echo "✅ $func defined<br>";
        } else {
            echo "❌ $func NOT found<br>";
        }
    }
    
    // Check for syntax errors in script
    echo "<h2>Script Syntax Check</h2>";
    
    // Count braces
    $open_braces = substr_count($script, '{');
    $close_braces = substr_count($script, '}');
    
    if ($open_braces !== $close_braces) {
        echo "⚠️ Brace mismatch! Opens: $open_braces, Closes: $close_braces<br>";
    } else {
        echo "✅ Braces match correctly ($open_braces pairs)<br>";
    }
    
    // Check for duplicate includes
    $analyzer_includes = substr_count($response, "seo_analyzer.js");
    if ($analyzer_includes > 1) {
        echo "❌ Duplicate seo_analyzer.js includes: $analyzer_includes<br>";
    } else {
        echo "✅ seo_analyzer.js included correctly: 1 time<br>";
    }
    
    // Check for invalid ending braces
    if (preg_match('/}\s*}\s*$/', $script)) {
        echo "⚠️ Possible duplicate closing braces at end<br>";
    }
    
} else {
    echo "Could not find script section";
}

// Test AJAX endpoints
echo "<h2>AJAX Endpoints Test</h2>";

// Test products
$prod_response = @file_get_contents('http://localhost/ecoms/admin_seo_manager.php', false, stream_context_create(['http' => ['method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => 'action=load_products']]));
if ($prod_response) {
    $prod_json = json_decode($prod_response, true);
    if ($prod_json && isset($prod_json['success']) && $prod_json['success']) {
        echo "✅ load_products works: " . $prod_json['total'] . " products<br>";
    } else {
        echo "❌ load_products failed<br>";
    }
}

// Test categories
$cat_response = @file_get_contents('http://localhost/ecoms/admin_seo_manager.php', false, stream_context_create(['http' => ['method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => 'action=load_categories']]));
if ($cat_response) {
    $cat_json = json_decode($cat_response, true);
    if ($cat_json && isset($cat_json['success']) && $cat_json['success']) {
        echo "✅ load_categories works: " . $cat_json['count'] . " categories<br>";
    } else {
        echo "❌ load_categories failed<br>";
    }
}

echo "<h2>✅ All tests completed</h2>";
?>
