<?php
// File: gallery.php (Dynamic Version)
include 'header.php';

// --- FETCH ALL PAINTINGS FOR THE GALLERY ---
$paintings = [];
$sql = "SELECT 
            p.id, 
            p.title, 
            p.image_path,
            CONCAT(ap.first_name, ' ', ap.last_name) as artist_name 
        FROM paintings p 
        LEFT JOIN artist_profiles ap ON p.artist_id = ap.user_id 
        ORDER BY p.uploaded_at DESC";

$result = $conn->query($sql);
if ($result) {
    $paintings = $result->fetch_all(MYSQLI_ASSOC);
}
?>

<div class="bg-white py-12 md:py-20">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <!-- Page Header -->
        <div class="text-center mb-12">
            <h1 class="text-4xl font-extrabold text-ucf-charcoal sm:text-5xl tracking-tight">Artwork Gallery</h1>
            <p class="mt-4 max-w-2xl mx-auto text-xl text-gray-500">
                A collection of incredible artwork from our community of talented artists.
            </p>
        </div>

        <!-- Gallery Grid -->
        <?php if (!empty($paintings)): ?>
            <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
                <?php foreach ($paintings as $painting): ?>
                    <div class="group relative block bg-black rounded-lg overflow-hidden">
                        <img alt="<?php echo htmlspecialchars($painting['title']); ?>"
                             src="<?php echo htmlspecialchars($painting['image_path']); ?>"
                             class="absolute inset-0 h-full w-full object-cover opacity-75 transition-opacity group-hover:opacity-50" />

                        <div class="relative p-4 sm:p-6 lg:p-8">
                            <div class="transform transition-all duration-500 group-hover:translate-y-8">
                                <p class="text-xl font-bold text-white sm:text-2xl"><?php echo htmlspecialchars($painting['title']); ?></p>
                                <p class="text-sm text-gray-200">by <?php echo htmlspecialchars(trim($painting['artist_name']) ?: 'Unknown Artist'); ?></p>
                            </div>

                            <div class="absolute bottom-4 left-4 sm:bottom-6 sm:left-6 lg:bottom-8 lg:left-8 mt-4 transform opacity-0 transition-all duration-500 group-hover:opacity-100 group-hover:translate-y-0">
                                <!-- This could link to a single painting detail page in the future -->
                                <a href="painting_single.php?id=<?php echo $painting['id']; ?>" class="inline-block bg-ucf-green text-white font-bold py-2 px-4 rounded-lg text-sm">
                                    View Details
                                </a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <div class="text-center py-16">
                <i class="fas fa-palette fa-4x text-gray-300 mb-4"></i>
                <h2 class="text-2xl font-bold text-ucf-charcoal">The Gallery is Empty</h2>
                <p class="text-gray-500 mt-2">No artwork has been uploaded yet. Please check back soon!</p>
            </div>
        <?php endif; ?>

    </div>
</div>

<?php include 'footer.php'; ?>

