<?php

declare(strict_types=1);

namespace App\Models;

use App\Core\Database;
use Throwable;

/**
 * Lecture des images publiées d'une galerie, dans l'ordre manuel.
 */
final class GalleryImage
{
    /** @return list<array<string,mixed>> */
    public static function publishedForGallery(int $galleryId): array
    {
        try {
            $pdo  = Database::connection();
            $stmt = $pdo->prepare(
                "SELECT id, name, alt, filename, width, height
                 FROM gallery_images
                 WHERE gallery_id = :gid AND status = 'published'
                 ORDER BY sort_order ASC, id ASC"
            );
            $stmt->execute(['gid' => $galleryId]);
            $rows = $stmt->fetchAll();
            return is_array($rows) ? $rows : [];
        } catch (Throwable) {
            return [];
        }
    }
}
