<?php

/**
 * Pagination Component
 *
 * Renders numbered page links for paginated result sets.
 * Stateless — receives all data from the calling view.
 *
 * Expected variable in scope:
 *   $pagination — array — Required. Shape:
 *     [
 *         'current'   => int,     // Current page number (1-indexed)
 *         'total'     => int,     // Total number of pages
 *         'base_url'  => string,  // Base URL e.g. '/app/couple/guests'
 *         'query'     => array,   // Extra query params to preserve (optional)
 *     ]
 *
 * If $pagination is not set or total <= 1, the component renders nothing.
 *
 * Usage in view files:
 *   <?php include ROOT_PATH . '/app/views/components/pagination.php'; ?>
 *
 * Usage from a controller (passing data to the view):
 *   $this->render('couple/guests/index', [
 *       'guests'     => $guests,
 *       'pagination' => [
 *           'current'  => $page,
 *           'total'    => $totalPages,
 *           'base_url' => '/app/couple/guests',
 *           'query'    => ['search' => $search],
 *       ],
 *   ]);
 *
 * @ref  INIT-011, DBP v1.0 Ch.8
 */

/** @var array<string, mixed>|null $pagination */
if (empty($pagination) || !is_array($pagination)) {
    return;
}

$currentPage = max(1, (int) ($pagination['current'] ?? 1));
$totalPages  = max(1, (int) ($pagination['total']   ?? 1));
$baseUrl     = (string) ($pagination['base_url'] ?? '');
$extraQuery  = is_array($pagination['query'] ?? null) ? $pagination['query'] : [];

// No need to render if only one page.
if ($totalPages <= 1) {
    return;
}

/**
 * Build a page URL with the correct page parameter and any extra query params.
 *
 * @param int    $page     Page number.
 * @param string $baseUrl  Base URL (path without query string).
 * @param array  $extra    Additional query parameters to merge.
 * @return string          Full URL with query string.
 */
$buildPageUrl = function (int $page, string $baseUrl, array $extra): string {
    $params = array_merge($extra, ['page' => $page]);
    return $baseUrl . '?' . http_build_query($params);
};

// Window: show at most 5 page links around the current page.
$windowSize  = 5;
$halfWindow  = (int) floor($windowSize / 2);
$windowStart = max(1, $currentPage - $halfWindow);
$windowEnd   = min($totalPages, $windowStart + $windowSize - 1);

// Adjust window start if we hit the end boundary.
if ($windowEnd - $windowStart < $windowSize - 1) {
    $windowStart = max(1, $windowEnd - $windowSize + 1);
}

?>
<nav class="pagination-nav" aria-label="Page navigation">
    <ul class="pagination">

        <!-- Previous page link -->
        <li>
            <?php if ($currentPage > 1): ?>
                <a
                    href="<?= escape_html($buildPageUrl($currentPage - 1, $baseUrl, $extraQuery)) ?>"
                    class="page-link"
                    aria-label="Previous page"
                    rel="prev"
                >
                    <i class="fa-solid fa-chevron-left text-[10px]"></i>
                </a>
            <?php else: ?>
                <span class="page-link disabled" aria-disabled="true" aria-label="Previous page (unavailable)">
                    <i class="fa-solid fa-chevron-left text-[10px]"></i>
                </span>
            <?php endif; ?>
        </li>

        <!-- First page + ellipsis -->
        <?php if ($windowStart > 1): ?>
            <li>
                <a
                    href="<?= escape_html($buildPageUrl(1, $baseUrl, $extraQuery)) ?>"
                    class="page-link"
                    aria-label="Page 1"
                >1</a>
            </li>
            <?php if ($windowStart > 2): ?>
                <li><span class="page-link disabled" aria-hidden="true">…</span></li>
            <?php endif; ?>
        <?php endif; ?>

        <!-- Page window -->
        <?php for ($p = $windowStart; $p <= $windowEnd; $p++): ?>
            <li>
                <?php if ($p === $currentPage): ?>
                    <span
                        class="page-link active"
                        aria-label="Page <?= $p ?>, current"
                        aria-current="page"
                    ><?= $p ?></span>
                <?php else: ?>
                    <a
                        href="<?= escape_html($buildPageUrl($p, $baseUrl, $extraQuery)) ?>"
                        class="page-link"
                        aria-label="Page <?= $p ?>"
                    ><?= $p ?></a>
                <?php endif; ?>
            </li>
        <?php endfor; ?>

        <!-- Last page + ellipsis -->
        <?php if ($windowEnd < $totalPages): ?>
            <?php if ($windowEnd < $totalPages - 1): ?>
                <li><span class="page-link disabled" aria-hidden="true">…</span></li>
            <?php endif; ?>
            <li>
                <a
                    href="<?= escape_html($buildPageUrl($totalPages, $baseUrl, $extraQuery)) ?>"
                    class="page-link"
                    aria-label="Page <?= $totalPages ?>"
                ><?= $totalPages ?></a>
            </li>
        <?php endif; ?>

        <!-- Next page link -->
        <li>
            <?php if ($currentPage < $totalPages): ?>
                <a
                    href="<?= escape_html($buildPageUrl($currentPage + 1, $baseUrl, $extraQuery)) ?>"
                    class="page-link"
                    aria-label="Next page"
                    rel="next"
                >
                    <i class="fa-solid fa-chevron-right text-[10px]"></i>
                </a>
            <?php else: ?>
                <span class="page-link disabled" aria-disabled="true" aria-label="Next page (unavailable)">
                    <i class="fa-solid fa-chevron-right text-[10px]"></i>
                </span>
            <?php endif; ?>
        </li>

    </ul>
</nav>
