<!-- Pagination -->
{{ $paginator := .Paginator }}
{{ $page := .Page }}
{{ if gt $paginator.TotalPages 1 }}
<nav>
    <ul class="pagination justify-content-center mt-5">
        <!-- Previous -->
        <li class="page-item {{ if not $paginator.HasPrev }}disabled{{ end }}">
            {{ if $paginator.HasPrev }}
                <a class="page-link" href="{{ $paginator.Prev.URL }}">&laquo; Précédent</a>
            {{ else }}
                <a class="page-link" href="#">&laquo; Précédent</a>
            {{ end }}
        </li>

        <!-- Page numbers -->
        {{ $currentPage := $paginator.PageNumber }}
        {{ $totalPages := $paginator.TotalPages }}
        
        <!-- Always show first page -->
        <li class="page-item {{ if eq $currentPage 1 }}active{{ end }}">
            <a class="page-link" href="{{ $paginator.First.URL }}">1</a>
        </li>

        <!-- Show ellipsis if current page is > 4 -->
        {{ if gt $currentPage 4 }}
            <li class="page-item disabled">
                <span class="page-link">...</span>
            </li>
        {{ end }}

        <!-- Show pages around current page -->
        {{ range $paginator.Pagers }}
            {{ $pageNumber := .PageNumber }}
            {{ if and (gt $pageNumber 1) (lt $pageNumber $totalPages) (ge $pageNumber (sub $currentPage 1)) (le $pageNumber (add $currentPage 1)) }}
                {{ if ne $pageNumber 1 }}
                    {{ if ne $pageNumber $totalPages }}
                        <li class="page-item {{ if eq $pageNumber $currentPage }}active{{ end }}">
                            <a class="page-link" href="{{ .URL }}">{{ $pageNumber }}</a>
                        </li>
                    {{ end }}
                {{ end }}
            {{ end }}
        {{ end }}

        <!-- Show ellipsis if current page is < totalPages - 3 -->
        {{ if lt $currentPage (sub $totalPages 3) }}
            <li class="page-item disabled">
                <span class="page-link">...</span>
            </li>
        {{ end }}

        <!-- Always show last page -->
        {{ if gt $totalPages 1 }}
            <li class="page-item {{ if eq $currentPage $totalPages }}active{{ end }}">
                <a class="page-link" href="{{ $paginator.Last.URL }}">{{ $totalPages }}</a>
            </li>
        {{ end }}

        <!-- Next -->
        <li class="page-item {{ if not $paginator.HasNext }}disabled{{ end }}">
            {{ if $paginator.HasNext }}
                <a class="page-link" href="{{ $paginator.Next.URL }}">Suivant &raquo;</a>
            {{ else }}
                <a class="page-link" href="#">Suivant &raquo;</a>
            {{ end }}
        </li>
    </ul>
</nav>
{{ end }}