// components/admin/AdminPagination.tsx
// Server-safe prev/next pagination for the admin tables. Preserves the current
// query params (search/filter/raffle) and only changes `offset`. "Siguiente" is
// enabled when the page fetched one extra row (hasNext), so no total count query
// is needed. Server component — just Links + spans, no client JS.

import * as React from "react";
import Link from "next/link";

export function AdminPagination({
  basePath,
  params,
  offset,
  pageSize,
  shown,
  hasNext,
}: {
  basePath: string;
  params: Record<string, string>;
  offset: number;
  pageSize: number;
  shown: number;
  hasNext: boolean;
}) {
  const href = (o: number) =>
    `${basePath}?${new URLSearchParams({ ...params, offset: String(o) }).toString()}`;
  const from = shown === 0 ? 0 : offset + 1;
  const to = offset + shown;

  return (
    <div className="mt-4 flex items-center justify-between gap-3">
      <span className="font-body text-caption text-muted">
        Mostrando {from}–{to}
      </span>
      <div className="flex gap-2">
        <PageLink disabled={offset <= 0} href={href(Math.max(0, offset - pageSize))}>
          Anterior
        </PageLink>
        <PageLink disabled={!hasNext} href={href(offset + pageSize)}>
          Siguiente
        </PageLink>
      </div>
    </div>
  );
}

function PageLink({
  disabled,
  href,
  children,
}: {
  disabled: boolean;
  href: string;
  children: React.ReactNode;
}) {
  const base =
    "inline-flex min-h-11 items-center rounded-md border px-4 font-body text-body-md focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary";
  if (disabled) {
    return (
      <span
        aria-disabled="true"
        className={`${base} border-hairline text-muted opacity-50`}
      >
        {children}
      </span>
    );
  }
  return (
    <Link
      href={href}
      className={`${base} border-primary text-primary hover:bg-primary hover:text-on-primary`}
    >
      {children}
    </Link>
  );
}
