import Link from 'next/link';

import SingleInfographic from "@/components/Infographics/SingleInfographic";
import Breadcrumb from "@/components/Common/Breadcrumb";

import { getInfographics } from '@/services/infographics';

import { Metadata } from "next";

export const metadata: Metadata = {
  title: "Infographics",
  description: "Infographic Page",
  // other metadata
};

type Props = {
  searchParams: Promise<{
    page?: string;
    topic?: string;
  }>;
};

export default async function Infographics({
  searchParams,
}: Props){

  const params = await searchParams;

  const currentPage = Number( params.page ) || 1;
  const currentTopic = params.topic || null;

  const result = await getInfographics(60, currentPage, currentTopic);
  const infographics = result.data;
  const pagination = result.pagination;
  const totalPages = pagination.last_page;
  const topics = result.topics;

  return (
    <>
      <Breadcrumb
        pageName="Infographics"
        description="Lorem ipsum dolor sit amet, consectetur adipiscing elit. In varius eros eget sapien consectetur ultrices. Ut quis dapibus libero."
      />

      <section>
        <div className="container">
          <div className="flex flex-wrap justify-center">
            <Link
              href={`?page=${currentPage - 1}`}
              className="bg-body-color/15 hover:bg-primary rounded-md px-4 py-2 m-3">
              All
            </Link>
            {topics.map((topic) => (
              <Link
                key={topic.id}
                href={`?page=${currentPage - 1}&topic=${topic.slug}`}
                className="bg-body-color/15 hover:bg-primary rounded-md px-4 py-2 m-3">
                <strong>{topic.title}</strong> <small>{topic.count}</small>
              </Link>
            ))}
          </div>
        </div>
      </section>

      <section className="pt-[30px] pb6-[120px]">
        <div className="container">
          <div className="-mx-4 flex flex-wrap justify-center">
            {infographics.map((infographic) => (
              <div
                key={infographic.id}
                className="w-full px-4 md:w-2/3 lg:w-1/2 xl:w-1/3"
              >
                <SingleInfographic infographic={infographic} />
              </div>
            ))}
          </div>

          <div className="-mx-4 mt-10 flex flex-wrap">
            <div className="w-full px-4">
              <ul className="flex items-center justify-center gap-2">
                { currentPage > 1 && (
                  <li>
                    <Link
                      href={`?page=${currentPage - 1}`}
                      className="bg-body-color/15 hover:bg-primary rounded-md px-4 py-2">
                      Prev
                    </Link>
                  </li>
                )}

                {Array.from(
                  { 
                    length: totalPages,
                  },
                  ( _, index ) => index + 1
                ).map(
                  ( page ) => (
                    <li key={ page } >
                      <Link
                        href={`?page=${page}`}
                        className={`flex h-9 min-w-[36px] items-center justify-center rounded-md px-4 transition
                        ${
                          page ===
                          currentPage
                            ? 'bg-primary text-white'
                            : 'bg-body-color/15 hover:bg-primary hover:text-white'
                        }
                      `}
                      >
                        {page}
                      </Link>
                    </li>
                  )
                )}

                {currentPage <
                  totalPages && (
                  <li>

                    <Link
                      href={`?page=${
                        currentPage +
                        1
                      }`}
                      className="
                      bg-body-color/15
                      hover:bg-primary
                      rounded-md
                      px-4
                      py-2
                    "
                    >
                      Next
                    </Link>
                  </li>
                )}
              </ul>
            </div>
          </div>
        </div>
      </section>
    </>
  );
};
