<?php
// Check what tables exist in the database

require_once __DIR__ . '/db_config.php';

echo "<h1>Database Tables Check</h1>";
echo "<p>Environment: " . ($is_local ? 'Local XAMPP' : 'cPanel') . "</p>";
echo "<p>Database: $DB_NAME</p>";

if ($conn->connect_error) {
    echo "<p style='color: red;'>Connection failed: " . $conn->connect_error . "</p>";
    exit();
}

echo "<p style='color: green;'>✅ Connected to database</p>";

// Get all tables
$result = $conn->query("SHOW TABLES");

if (!$result) {
    echo "<p style='color: red;'>Error getting tables: " . $conn->error . "</p>";
    exit();
}

echo "<h2>Available Tables:</h2>";
echo "<ul>";

while ($row = $result->fetch_row()) {
    echo "<li>" . htmlspecialchars($row[0]) . "</li>";
}

echo "</ul>";

// Check specific tables we expect
echo "<h2>Expected Tables Status:</h2>";
$expected = ['products', 'blogs', 'users', 'orders', 'categories'];
foreach ($expected as $table) {
    $check = $conn->query("SHOW TABLES LIKE '$table'");
    $exists = ($check && $check->num_rows > 0) ? '✅ EXISTS' : '❌ NOT FOUND';
    echo "<p>$table: $exists</p>";
}

// Get table structure for products if it exists
echo "<h2>Table Structure (if exists):</h2>";
$result = $conn->query("SHOW TABLES LIKE 'products'");
if ($result && $result->num_rows > 0) {
    echo "<h3>Products Table:</h3>";
    $columns = $conn->query("DESCRIBE products");
    echo "<ul>";
    while ($col = $columns->fetch_assoc()) {
        echo "<li>" . htmlspecialchars($col['Field']) . " (" . htmlspecialchars($col['Type']) . ")</li>";
    }
    echo "</ul>";
}

$conn->close();
?>
