<?php if (!empty($pagination) && ($pagination['total_pages'] ?? 1) > 1): ?>
    <?php
    $currentPage = (int) $pagination['current_page'];
    $totalPages = (int) $pagination['total_pages'];
    $startPage = max(1, $currentPage - 2);
    $endPage = min($totalPages, $currentPage + 2);
    ?>
    <nav class="pagination" aria-label="Pagination">
        <span>
            <?= (int) $pagination['total_items'] ?> resultat(s)
            - page <?= $currentPage ?> / <?= $totalPages ?>
        </span>
        <div>
            <?php if ($currentPage > 1): ?>
                <a href="<?= e(pagination_url($currentPage - 1, $pagination)) ?>">Precedent</a>
            <?php endif; ?>

            <?php if ($startPage > 1): ?>
                <a href="<?= e(pagination_url(1, $pagination)) ?>">1</a>
                <?php if ($startPage > 2): ?><em>...</em><?php endif; ?>
            <?php endif; ?>

            <?php for ($page = $startPage; $page <= $endPage; $page++): ?>
                <?php if ($page === $currentPage): ?>
                    <strong><?= $page ?></strong>
                <?php else: ?>
                    <a href="<?= e(pagination_url($page, $pagination)) ?>"><?= $page ?></a>
                <?php endif; ?>
            <?php endfor; ?>

            <?php if ($endPage < $totalPages): ?>
                <?php if ($endPage < $totalPages - 1): ?><em>...</em><?php endif; ?>
                <a href="<?= e(pagination_url($totalPages, $pagination)) ?>"><?= $totalPages ?></a>
            <?php endif; ?>

            <?php if ($currentPage < $totalPages): ?>
                <a href="<?= e(pagination_url($currentPage + 1, $pagination)) ?>">Suivant</a>
            <?php endif; ?>
        </div>
    </nav>
<?php endif; ?>
