<?php
/**
 * Pagination partial
 *
 * Required variables (set before include):
 *   int   $page        — current page (1-based)
 *   int   $totalPages  — total number of pages
 *   array $baseParams  — query-string params preserved across page links
 *                        (e.g. ['year' => 2024, 'query' => 'design'])
 *                        The 'page' key is injected automatically; do not include it.
 *
 * Usage:
 *   <?php $baseParams = array_filter(['year' => $year]); ?>
 *   <?php include APP_ROOT . '/templates/partials/pagination.php'; ?>
 */

if (!isset($page, $totalPages) || $totalPages <= 1) {
    return; // nothing to render
}

$baseParams = isset($baseParams) && is_array($baseParams) ? $baseParams : [];

/** Build a URL for a given target page, preserving all base params. */
$paginationUrl = static function(int $targetPage) use ($baseParams): string {
    return '?' . http_build_query(array_merge($baseParams, ['page' => $targetPage]));
};

$atFirst = $page <= 1;
$atLast  = $page >= $totalPages;
?>
<nav class="pagination-wrap" aria-label="Pagination">
    <ul>
        <li>
            <a href="<?= $paginationUrl(1) ?>"
               class="pagination-btn<?= $atFirst ? ' disabled' : '' ?>"
               <?= $atFirst ? 'aria-disabled="true" tabindex="-1"' : '' ?>
               aria-label="Première page">«</a>
        </li>
        <li>
            <a href="<?= $paginationUrl(max(1, $page - 1)) ?>"
               class="pagination-btn<?= $atFirst ? ' disabled' : '' ?>"
               <?= $atFirst ? 'aria-disabled="true" tabindex="-1"' : '' ?>
               aria-label="Page précédente">‹</a>
        </li>
        <li class="pagination-info" aria-current="page">
            <span class="page-current"><?= $page ?></span> / <?= $totalPages ?>
        </li>
        <li>
            <a href="<?= $paginationUrl(min($totalPages, $page + 1)) ?>"
               class="pagination-btn<?= $atLast ? ' disabled' : '' ?>"
               <?= $atLast ? 'aria-disabled="true" tabindex="-1"' : '' ?>
               aria-label="Page suivante">›</a>
        </li>
        <li>
            <a href="<?= $paginationUrl($totalPages) ?>"
               class="pagination-btn<?= $atLast ? ' disabled' : '' ?>"
               <?= $atLast ? 'aria-disabled="true" tabindex="-1"' : '' ?>
               aria-label="Dernière page">»</a>
        </li>
    </ul>
</nav>
