<?php

if (!function_exists('legacy_page_number')) {
    function legacy_page_number(string $param = 'p'): int
    {
        $page = (int)($_GET[$param] ?? 1);
        return $page > 0 ? $page : 1;
    }
}

if (!function_exists('legacy_page_limit')) {
    function legacy_page_limit(string $param = 'limit', int $default = 50, array $allowed = [25, 50, 100, 200]): int
    {
        $limit = (int)($_GET[$param] ?? $default);
        return in_array($limit, $allowed, true) ? $limit : $default;
    }
}

if (!function_exists('legacy_escape_like')) {
    function legacy_escape_like(mysqli $db, string $value): string
    {
        $value = str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $value);
        return mysqli_real_escape_string($db, $value);
    }
}

if (!function_exists('legacy_fetch_stats')) {
    function legacy_fetch_stats(mysqli $db, string $sql): array
    {
        $result = mysqli_query($db, $sql);
        if (!$result) {
            die('Query gagal: ' . mysqli_error($db));
        }

        return mysqli_fetch_assoc($result) ?: [];
    }
}

if (!function_exists('legacy_render_pagination')) {
    function legacy_render_pagination(string $pageName, string $pageParam, int $currentPage, int $totalPages, array $params = []): void
    {
        if ($totalPages <= 1) {
            return;
        }

        $currentPage = max(1, min($currentPage, $totalPages));
        $start = max(1, $currentPage - 2);
        $end = min($totalPages, $currentPage + 2);

        $urlFor = static function (int $page) use ($pageName, $pageParam, $params): string {
            $query = array_merge($params, [$pageParam => $page]);

            if (function_exists('page_url')) {
                return page_url($pageName, $query);
            }

            return '?' . http_build_query(array_merge(['page' => $pageName], $query));
        };
        ?>
        <nav class="mt-3" aria-label="Pagination">
            <ul class="pagination pagination-sm mb-0 flex-wrap">
                <li class="page-item <?= $currentPage <= 1 ? 'disabled' : '' ?>">
                    <a class="page-link" href="<?= htmlspecialchars($urlFor(max(1, $currentPage - 1)), ENT_QUOTES, 'UTF-8') ?>">Prev</a>
                </li>
                <?php if ($start > 1): ?>
                    <li class="page-item"><a class="page-link" href="<?= htmlspecialchars($urlFor(1), ENT_QUOTES, 'UTF-8') ?>">1</a></li>
                    <?php if ($start > 2): ?><li class="page-item disabled"><span class="page-link">...</span></li><?php endif; ?>
                <?php endif; ?>
                <?php for ($i = $start; $i <= $end; $i++): ?>
                    <li class="page-item <?= $i === $currentPage ? 'active' : '' ?>">
                        <a class="page-link" href="<?= htmlspecialchars($urlFor($i), ENT_QUOTES, 'UTF-8') ?>"><?= number_format($i) ?></a>
                    </li>
                <?php endfor; ?>
                <?php if ($end < $totalPages): ?>
                    <?php if ($end < $totalPages - 1): ?><li class="page-item disabled"><span class="page-link">...</span></li><?php endif; ?>
                    <li class="page-item"><a class="page-link" href="<?= htmlspecialchars($urlFor($totalPages), ENT_QUOTES, 'UTF-8') ?>"><?= number_format($totalPages) ?></a></li>
                <?php endif; ?>
                <li class="page-item <?= $currentPage >= $totalPages ? 'disabled' : '' ?>">
                    <a class="page-link" href="<?= htmlspecialchars($urlFor(min($totalPages, $currentPage + 1)), ENT_QUOTES, 'UTF-8') ?>">Next</a>
                </li>
            </ul>
        </nav>
        <?php
    }
}
