<?php

namespace lib\Utils;

class Pagination
{
  public int $currentPage;
  public int $perPage;
  public int $totalItems;
  public int $totalPages;
  public int $start;
  public int $end;

  public function __construct(int $totalItems, int $currentPage = 1, int $perPage = 10)
  {
    $this->totalItems = $totalItems;
    $this->perPage = $perPage;
    $this->currentPage = max(1, $currentPage);
    $this->totalPages = (int)ceil($totalItems / $perPage);

    $this->start = ($this->currentPage - 1) * $perPage;
    $this->end = min($this->start + $perPage, $totalItems);
  }

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

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

  public function previousPage(): int
  {
    return max(1, $this->currentPage - 1);
  }

  public function nextPage(): int
  {
    return min($this->totalPages, $this->currentPage + 1);
  }

  public function getOffset(): int
  {
    return $this->start;
  }

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

  public function toArray(): array
  {
    return [
      'current_page' => $this->currentPage,
      'per_page' => $this->perPage,
      'total_items' => $this->totalItems,
      'total_pages' => $this->totalPages,
      'has_previous' => $this->hasPrevious(),
      'has_next' => $this->hasNext(),
      'previous_page' => $this->previousPage(),
      'next_page' => $this->nextPage(),
      'offset' => $this->getOffset(),
      'limit' => $this->getLimit(),
    ];
  }

  public function render(string $baseUrl = '', array $query = []): string
  {
    if ($this->totalPages <= 1) {
      return ''; // 페이지가 1개 이하면 렌더링 생략
    }

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

    // 이전 버튼
    if ($this->hasPrevious()) {
      $html .= $this->pageLink($this->previousPage(), '&laquo;', $baseUrl, $query);
    } else {
      $html .= '<li class="page-item disabled"><span class="page-link">&laquo;</span></li>';
    }

    // 페이지 번호 링크 (간단히 1 ~ totalPages 모두 표시, 필요시 range 조절 가능)
    for ($i = 1; $i <= $this->totalPages; $i++) {
      if ($i === $this->currentPage) {
        $html .= '<li class="page-item active"><span class="page-link">' . $i . '</span></li>';
      } else {
        $html .= $this->pageLink($i, (string)$i, $baseUrl, $query);
      }
    }

    // 다음 버튼
    if ($this->hasNext()) {
      $html .= $this->pageLink($this->nextPage(), '&raquo;', $baseUrl, $query);
    } else {
      $html .= '<li class="page-item disabled"><span class="page-link">&raquo;</span></li>';
    }

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

  private function pageLink(int $page, string $label, string $baseUrl, array $query): string
  {
    $query['page'] = $page;
    $url = $baseUrl . '?' . http_build_query($query);
    return '<li class="page-item"><a class="page-link" href="' . htmlspecialchars($url) . '">' . $label . '</a></li>';
  }
}
