/**
 * BlogPagination - Editorial-style pagination component
 *
 * Provides navigation between paginated blog pages with
 * previous/next buttons and numbered page links.
 */

import Link from "next/link";
import { BLOG_COLORS, getBlogTranslations, padNumber } from "@/lib/blog.config";

export interface BlogPaginationProps {
  /** Current active page (1-based) */
  currentPage: number;
  /** Total number of pages */
  totalPages: number;
  /** Language for localization */
  lang: "ar" | "en";
  /** Base URL path (default: blog) */
  basePath?: string;
}

export default function BlogPagination({
  currentPage,
  totalPages,
  lang,
  basePath = "blog",
}: BlogPaginationProps) {
  const t = getBlogTranslations(lang);
  const isAr = lang === "ar";

  if (totalPages <= 1) {
    return null;
  }

  return (
    <nav
      className="mt-16 flex items-center justify-center gap-2"
      aria-label={isAr ? "التنقل بين الصفحات" : "Page navigation"}
    >
      {/* Previous Button */}
      {currentPage > 1 && (
        <Link
          href={`/${lang}/${basePath}?page=${currentPage - 1}`}
          className="group flex items-center gap-2 px-6 py-3 border-2 font-semibold transition-colors duration-300"
          style={{
            borderColor: BLOG_COLORS.heading,
            color: BLOG_COLORS.heading,
          }}
          aria-label={t.previous}
        >
          <ChevronIcon direction={isAr ? "right" : "left"} />
          {t.previous}
        </Link>
      )}

      {/* Page Numbers */}
      <div className="flex items-center gap-1 px-4" role="list">
        {Array.from({ length: totalPages }, (_, i) => i + 1).map((page) => (
          <Link
            key={page}
            href={`/${lang}/${basePath}?page=${page}`}
            className="w-10 h-10 flex items-center justify-center font-mono text-sm transition-colors duration-300"
            style={{
              backgroundColor:
                currentPage === page ? BLOG_COLORS.primary : "transparent",
              color: currentPage === page ? "white" : BLOG_COLORS.body,
            }}
            aria-label={`${isAr ? "صفحة" : "Page"} ${page}`}
            aria-current={currentPage === page ? "page" : undefined}
          >
            {padNumber(page)}
          </Link>
        ))}
      </div>

      {/* Next Button */}
      {currentPage < totalPages && (
        <Link
          href={`/${lang}/${basePath}?page=${currentPage + 1}`}
          className="group flex items-center gap-2 px-6 py-3 border-2 font-semibold transition-colors duration-300"
          style={{
            borderColor: BLOG_COLORS.heading,
            color: BLOG_COLORS.heading,
          }}
          aria-label={t.next}
        >
          {t.next}
          <ChevronIcon direction={isAr ? "left" : "right"} />
        </Link>
      )}
    </nav>
  );
}

/** Chevron icon component for pagination buttons */
function ChevronIcon({ direction }: { direction: "left" | "right" }) {
  return (
    <svg
      className={`w-4 h-4 ${direction === "left" ? "rotate-180" : ""}`}
      fill="none"
      viewBox="0 0 24 24"
      stroke="currentColor"
      aria-hidden="true"
    >
      <path
        strokeLinecap="round"
        strokeLinejoin="round"
        strokeWidth={2}
        d="M9 5l7 7-7 7"
      />
    </svg>
  );
}
