<?php
require_once 'includes/config.php';

// Check if gallery_images table exists
$table_check = $conn->query("SHOW TABLES LIKE 'gallery_images'");

if ($table_check->num_rows == 0) {
    die("The 'gallery_images' table does not exist in the database.");
}

// Get the structure of the gallery_images table
$result = $conn->query("DESCRIBE gallery_images");
echo "<h3>Gallery Images Table Structure:</h3>";
echo "<table border='1' cellpadding='5'>";
echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>";
while($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $row['Field'] . "</td>";
    echo "<td>" . $row['Type'] . "</td>";
    echo "<td>" . $row['Null'] . "</td>";
    echo "<td>" . $row['Key'] . "</td>";
    echo "<td>" . ($row['Default'] ?? 'NULL') . "</td>";
    echo "<td>" . $row['Extra'] . "</td>";
    echo "</tr>";
}
echo "</table>";

// Show sample data
$sample = $conn->query("SELECT * FROM gallery_images LIMIT 5");
if ($sample->num_rows > 0) {
    echo "<h3>Sample Data (first 5 records):</h3>";
    echo "<table border='1' cellpadding='5'>";
    $first = true;
    while($row = $sample->fetch_assoc()) {
        if ($first) {
            // Header row
            echo "<tr>";
            foreach(array_keys($row) as $key) {
                echo "<th>" . htmlspecialchars($key) . "</th>";
            }
            echo "</tr>";
            $first = false;
        }
        // Data row
        echo "<tr>";
        foreach($row as $value) {
            echo "<td>" . htmlspecialchars(substr($value ?? '', 0, 50)) . "</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "<p>No records found in gallery_images table.</p>";
}

$conn->close();
?>

<h3>Image Directory Check:</h3>
<?php
$image_dir = realpath(dirname(__FILE__) . '/../images');
if (is_dir($image_dir)) {
    echo "<p>Image directory exists at: " . htmlspecialchars($image_dir) . "</p>";
    
    // Check for gallery subdirectory
    $gallery_dir = $image_dir . '/gallery';
    if (is_dir($gallery_dir)) {
        echo "<p>Gallery directory exists at: " . htmlspecialchars($gallery_dir) . "</p>";
        
        // Count images in the gallery directory
        $images = glob($gallery_dir . '/*.{jpg,jpeg,png,gif,webp}', GLOB_BRACE);
        echo "<p>Found " . count($images) . " images in the gallery directory.</p>";
        
        // Show first 5 images as example
        if (count($images) > 0) {
            echo "<h4>Sample Images:</h4><div style='display: flex; flex-wrap: wrap; gap: 10px;'>";
            $count = 0;
            foreach(array_slice($images, 0, 5) as $image) {
                $rel_path = str_replace(realpath($_SERVER['DOCUMENT_ROOT']), '', realpath($image));
                $rel_path = str_replace('\\', '/', $rel_path);
                echo "<div style='border: 1px solid #ddd; padding: 5px; text-align: center;'>";
                echo "<img src='" . htmlspecialchars($rel_path) . "' style='max-width: 150px; max-height: 100px;'><br>";
                echo "<small>" . htmlspecialchars(basename($image)) . "</small>";
                echo "</div>";
                $count++;
                if ($count >= 5) break;
            }
            echo "</div>";
        }
    } else {
        echo "<p style='color: red;'>Gallery directory does not exist at: " . htmlspecialchars($gallery_dir) . "</p>";
    }
} else {
    echo "<p style='color: red;'>Image directory does not exist at: " . htmlspecialchars($image_dir) . "</p>";
}
?>
