"use client";

import { useRouter, useSearchParams } from "next/navigation";
import { ChevronLeft, ChevronRight } from "lucide-react";

interface PaginationProps {
    currentPage: number;
    totalPages: number;
    total: number;
    pageSize: number;
}

export function Pagination({ currentPage, totalPages, total, pageSize }: PaginationProps) {
    const router = useRouter();
    const searchParams = useSearchParams();

    if (totalPages <= 1) return null;

    const goToPage = (page: number) => {
        const params = new URLSearchParams(searchParams.toString());
        if (page <= 1) params.delete("page"); else params.set("page", page.toString());
        router.push(`/jobs?${params.toString()}`);
        window.scrollTo({ top: 0, behavior: "smooth" });
    };

    // Generate page numbers to show
    const pages: (number | "...")[] = [];
    const maxVisible = 5;

    if (totalPages <= maxVisible + 2) {
        for (let i = 1; i <= totalPages; i++) pages.push(i);
    } else {
        pages.push(1);
        if (currentPage > 3) pages.push("...");
        const start = Math.max(2, currentPage - 1);
        const end = Math.min(totalPages - 1, currentPage + 1);
        for (let i = start; i <= end; i++) pages.push(i);
        if (currentPage < totalPages - 2) pages.push("...");
        pages.push(totalPages);
    }

    const showingStart = (currentPage - 1) * pageSize + 1;
    const showingEnd = Math.min(currentPage * pageSize, total);

    return (
        <div className="flex flex-col items-center gap-4 pt-8">
            <p className="text-sm text-slate-500">
                Showing <span className="font-semibold text-slate-700">{showingStart}–{showingEnd}</span> of{" "}
                <span className="font-semibold text-slate-700">{total.toLocaleString()}</span> jobs
            </p>

            <nav className="flex items-center gap-1" aria-label="Pagination">
                <button
                    onClick={() => goToPage(currentPage - 1)}
                    disabled={currentPage <= 1}
                    className="flex h-9 w-9 items-center justify-center rounded-lg border border-slate-200 bg-white text-slate-500 hover:bg-slate-50 transition-colors disabled:opacity-40 disabled:cursor-not-allowed"
                    aria-label="Previous page"
                >
                    <ChevronLeft className="h-4 w-4" />
                </button>

                {pages.map((page, i) =>
                    page === "..." ? (
                        <span key={`dots-${i}`} className="flex h-9 w-9 items-center justify-center text-sm text-slate-400">
                            ⋯
                        </span>
                    ) : (
                        <button
                            key={page}
                            onClick={() => goToPage(page)}
                            className={`flex h-9 w-9 items-center justify-center rounded-lg text-sm font-semibold transition-all ${page === currentPage
                                    ? "bg-blue-600 text-white shadow-sm shadow-blue-200"
                                    : "border border-slate-200 bg-white text-slate-700 hover:bg-slate-50"
                                }`}
                            aria-current={page === currentPage ? "page" : undefined}
                        >
                            {page}
                        </button>
                    )
                )}

                <button
                    onClick={() => goToPage(currentPage + 1)}
                    disabled={currentPage >= totalPages}
                    className="flex h-9 w-9 items-center justify-center rounded-lg border border-slate-200 bg-white text-slate-500 hover:bg-slate-50 transition-colors disabled:opacity-40 disabled:cursor-not-allowed"
                    aria-label="Next page"
                >
                    <ChevronRight className="h-4 w-4" />
                </button>
            </nav>
        </div>
    );
}
