<?php
/**
 * Debug script to check SEO system status
 */

require_once 'db_config.php';

echo "=== SEO System Status Check ===\n\n";

// Check database connection
$db = $conn;  // Use the connection from db_config.php
if ($db->connect_error) {
    die("DB Connection Failed: " . $db->connect_error);
}
echo "✓ Database connected\n";

// Check if SEO tables exist
$tables = ['seo_settings', 'seo_analytics'];
foreach ($tables as $table) {
    $result = $db->query("SHOW TABLES LIKE '$table'");
    if ($result && $result->num_rows > 0) {
        echo "✓ Table '$table' exists\n";
    } else {
        echo "✗ Table '$table' missing\n";
    }
}

// Check files
$files = [
    'seo_plugin.php',
    'seo_schema_generator.php',
    'seo_readability.php',
    'seo_keywords.php',
    'seo_analytics.php',
    'data/products.json'
];

echo "\n=== File Check ===\n";
foreach ($files as $file) {
    if (file_exists($file)) {
        echo "✓ $file exists\n";
    } else {
        echo "✗ $file missing\n";
    }
}

// Test schema generator
echo "\n=== Schema Generator Test ===\n";
require_once 'seo_schema_generator.php';
$schema = SchemaMarkupGenerator::generateSchema('product', [
    'name' => 'Test Product',
    'description' => 'Test Description',
    'price' => 100,
    'url' => '/product.php?id=1'
]);

if (is_string($schema)) {
    echo "✓ Schema generation returns string\n";
    $decoded = json_decode($schema, true);
    if ($decoded) {
        echo "✓ Schema is valid JSON\n";
        echo "  Schema Type: " . $decoded['@type'] . "\n";
    } else {
        echo "✗ Schema is not valid JSON\n";
    }
} else {
    echo "✗ Schema generation didn't return string\n";
}

// Test products JSON
echo "\n=== Products JSON Test ===\n";
$products_json = file_get_contents('data/products.json');
$products = json_decode($products_json, true);
if (is_array($products)) {
    echo "✓ Products JSON is valid\n";
    echo "  Total Products: " . count($products) . "\n";
    if (count($products) > 0) {
        $first = $products[0];
        echo "  First Product ID: " . ($first['id'] ?? 'missing') . "\n";
        echo "  First Product Title: " . ($first['title'] ?? 'missing') . "\n";
    }
} else {
    echo "✗ Products JSON is not valid\n";
}

echo "\n=== All Checks Complete ===\n";
?>
