<?php
/**
 * Gallery API Endpoint
 */

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

require_once __DIR__ . '/../config/database.php';

try {
    $pdo = getDBConnection();
    
    $stmt = $pdo->query("
        SELECT id, filename, caption, alt_text, created_at
        FROM gallery_images
        WHERE status = 'active'
        ORDER BY created_at DESC
    ");
    
    $images = [];
    while ($row = $stmt->fetch()) {
        $images[] = [
            'id' => $row['id'],
            'url' => '/backend/uploads/gallery/' . $row['filename'],
            'caption' => $row['caption'],
            'alt' => $row['alt_text'] ?: $row['caption']
        ];
    }
    
    echo json_encode([
        'success' => true,
        'images' => $images
    ]);
    
} catch (Exception $e) {
    error_log("Gallery API error: " . $e->getMessage());
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'images' => []
    ]);
}

