<?php
/**
 * Pagination Helper
 */

class Pagination
{
    private int $totalItems;
    private int $perPage;
    private int $currentPage;
    private int $totalPages;

    public function __construct(int $totalItems, int $perPage = ITEMS_PER_PAGE, int $currentPage = 1)
    {
        $this->totalItems = $totalItems;
        $this->perPage = $perPage > 0 ? $perPage : ITEMS_PER_PAGE;
        $this->totalPages = max(1, (int) ceil($totalItems / $this->perPage));
        $this->currentPage = max(1, min($currentPage, $this->totalPages));
    }

    public function getOffset(): int
    {
        return ($this->currentPage - 1) * $this->perPage;
    }

    public function getLimit(): int
    {
        return $this->perPage;
    }

    public function getCurrentPage(): int
    {
        return $this->currentPage;
    }

    public function getTotalPages(): int
    {
        return $this->totalPages;
    }

    public function getTotalItems(): int
    {
        return $this->totalItems;
    }

    public function hasPrev(): bool
    {
        return $this->currentPage > 1;
    }

    public function hasNext(): bool
    {
        return $this->currentPage < $this->totalPages;
    }

    public function render(string $baseUrl, string $queryParam = 'page'): string
    {
        if ($this->totalPages <= 1) {
            return '';
        }

        $html = '<nav class="pagination-nav"><ul class="pagination">';

        // Previous
        if ($this->hasPrev()) {
            $html .= '<li class="page-item"><a class="page-link" href="' . $this->buildUrl($baseUrl, $queryParam, $this->currentPage - 1) . '">&laquo; Previous</a></li>';
        } else {
            $html .= '<li class="page-item disabled"><span class="page-link">&laquo; Previous</span></li>';
        }

        // Page numbers
        $start = max(1, $this->currentPage - 2);
        $end = min($this->totalPages, $this->currentPage + 2);

        if ($start > 1) {
            $html .= '<li class="page-item"><a class="page-link" href="' . $this->buildUrl($baseUrl, $queryParam, 1) . '">1</a></li>';
            if ($start > 2) {
                $html .= '<li class="page-item disabled"><span class="page-link">...</span></li>';
            }
        }

        for ($i = $start; $i <= $end; $i++) {
            if ($i === $this->currentPage) {
                $html .= '<li class="page-item active"><span class="page-link">' . $i . '</span></li>';
            } else {
                $html .= '<li class="page-item"><a class="page-link" href="' . $this->buildUrl($baseUrl, $queryParam, $i) . '">' . $i . '</a></li>';
            }
        }

        if ($end < $this->totalPages) {
            if ($end < $this->totalPages - 1) {
                $html .= '<li class="page-item disabled"><span class="page-link">...</span></li>';
            }
            $html .= '<li class="page-item"><a class="page-link" href="' . $this->buildUrl($baseUrl, $queryParam, $this->totalPages) . '">' . $this->totalPages . '</a></li>';
        }

        // Next
        if ($this->hasNext()) {
            $html .= '<li class="page-item"><a class="page-link" href="' . $this->buildUrl($baseUrl, $queryParam, $this->currentPage + 1) . '">Next &raquo;</a></li>';
        } else {
            $html .= '<li class="page-item disabled"><span class="page-link">Next &raquo;</span></li>';
        }

        $html .= '</ul></nav>';
        return $html;
    }

    private function buildUrl(string $baseUrl, string $param, int $page): string
    {
        $separator = str_contains($baseUrl, '?') ? '&' : '?';
        return $baseUrl . $separator . $param . '=' . $page;
    }
}
