"use client"

import * as React from "react"
import { useTranslation } from "react-i18next"
import type { Table } from "@tanstack/react-table"
import { Button } from "@/components/ui/button"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { cn } from "@/lib/utils"

interface DataTablePaginationProps<TData> {
  table: Table<TData>
  pageSizeOptions?: number[]
  className?: string
}

export function DataTablePagination<TData>({
  table,
  pageSizeOptions = [10, 20, 50, 100],
  className,
}: DataTablePaginationProps<TData>) {
  const { t } = useTranslation()
  const pagination = table.getState().pagination
  const currentPage = pagination.pageIndex + 1
  const pageCount = table.getPageCount()
  const canPrevious = table.getCanPreviousPage()
  const canNext = table.getCanNextPage()

  const handlePageSizeChange = (value: string) => {
    const parsed = Number.parseInt(value, 10)
    if (!Number.isNaN(parsed)) {
      table.setPageSize(parsed)
    }
  }

  return (
    <div className={cn("flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between", className)}>
      <div className="flex items-center gap-3">
        <span className="text-sm text-muted-foreground">{t("Rows per page")}</span>
        <Select value={String(pagination.pageSize)} onValueChange={(value) => handlePageSizeChange(value ?? "")}>
          <SelectTrigger className="h-10 w-24 sm:h-8" aria-label={t("Rows per page")}>
            <SelectValue />
          </SelectTrigger>
          <SelectContent>
            {pageSizeOptions.map((option) => (
              <SelectItem key={option} value={String(option)}>
                {String(option)}
              </SelectItem>
            ))}
          </SelectContent>
        </Select>
      </div>

      <div className="flex min-w-0 flex-col gap-2 sm:flex-row sm:items-center">
        <span className="text-sm text-muted-foreground" role="status" aria-live="polite">
          {t("Page {current} of {total}", {
            current: pageCount === 0 ? 0 : currentPage,
            total: pageCount,
          })}
        </span>
        <div className="grid w-full grid-cols-2 gap-2 sm:flex sm:w-auto sm:items-center">
          <Button
            variant="outline"
            size="sm"
            className="h-10 w-full sm:h-7 sm:w-auto"
            disabled={!canPrevious}
            onClick={() => table.setPageIndex(0)}
          >
            {t("First")}
          </Button>
          <Button
            variant="outline"
            size="sm"
            className="h-10 w-full sm:h-7 sm:w-auto"
            disabled={!canPrevious}
            onClick={() => table.previousPage()}
          >
            {t("Prev")}
          </Button>
          <Button
            variant="outline"
            size="sm"
            className="h-10 w-full sm:h-7 sm:w-auto"
            disabled={!canNext}
            onClick={() => table.nextPage()}
          >
            {t("Next")}
          </Button>
          <Button
            variant="outline"
            size="sm"
            className="h-10 w-full sm:h-7 sm:w-auto"
            disabled={!canNext}
            onClick={() => table.setPageIndex(Math.max(pageCount - 1, 0))}
          >
            {t("Last")}
          </Button>
        </div>
      </div>
    </div>
  )
}
