"use client";

import Link from "next/link";
import styles from "./Pagination.module.css";
import Badge from "@/components/Badge/Badge";

type PaginationProps = {
  totalPages: number;
  currentPage: number;
  basePath?: string;
};

const Pagination = ({
  totalPages,
  currentPage = 2,
  basePath = "/insights",
}: PaginationProps) => {
  const getNextPageLink = (): string => {
    if (currentPage === totalPages) return "";

    return `${basePath}/page/${Math.max(currentPage + 1)}`;
  };

  const getPreviousPageLink = (): string => {
    if (currentPage === 1) return "";

    let url = `${basePath}/page/${Math.max(2, currentPage - 1)}`;

    if (currentPage === 2) {
      url = basePath;
    }

    return url;
  };

  return (
    <div className={styles.pagination}>
      <Link href={getPreviousPageLink()}>
        <Badge
          className={currentPage === 1 ? "opacity-30 pointer-events-none" : ""}
        >
          <svg
          xmlns="http://www.w3.org/2000/svg"
          height="24"
          viewBox="0 -960 960 960"
          width="24"
        >
          <path d="m313-440 224 224-57 56-320-320 320-320 57 56-224 224h487v80H313Z" />
        </svg>
        </Badge>
      </Link>
      <h3 className="m-none mx-sm">
        {currentPage} of {totalPages}
      </h3>
      <Link href={getNextPageLink()}>
      <Badge
        className={
          currentPage === totalPages ? "opacity-30 pointer-events-none" : ""
        }
      >
        <svg
          xmlns="http://www.w3.org/2000/svg"
          height="24"
          viewBox="0 -960 960 960"
          width="24"
        >
          <path d="M647-440H160v-80h487L423-744l57-56 320 320-320 320-57-56 224-224Z" />
        </svg>
      </Badge>
      </Link>
    </div>
  );
};

export default Pagination;
