import { notFound } from "next/navigation";
import type { Metadata } from "next";
import Pagination from "@/components/Pagination/Pagination";
import Listing from "@/components/Listing/Listing";
import { getCachedPSPosts } from "@/services/prestashop-wp";

const PaginatedInsightsAndResources = async (props: {
  params: Promise<{ page: string }>;
}) => {
  const params = await props.params;
  const currentPage = parseInt(params.page) || 2;
  const perPage = parseInt(process.env.BLOG_PAGINATION_PER_PAGE!) || 6;
  const posts = await getCachedPSPosts(currentPage, perPage);

  if (posts.posts.length === 0) {
    notFound();
  }

  const totalPages = Math.ceil(posts._paging.total / perPage);

  return (
    <Listing posts={posts.posts} postBasePath="/insights-and-resources">
      <div className="mt-sm col-span-full flex items-center justify-center">
        <Pagination
          totalPages={totalPages}
          currentPage={currentPage}
          basePath="/insights-and-resources"
        />
      </div>
    </Listing>
  );
};

export const generateMetadata = async (props: {
  params: Promise<{ page: string }>;
}): Promise<Metadata> => {
  const params = await props.params;
  const baseUrl = process.env.SITE_URL || "https://teapot.agency";
  const canonicalUrl = `${baseUrl}prestashop/insights-and-resources`;

  return {
    title: `Insights & Resources - page ${params.page} | Teapot Creative`,
    alternates: {
      canonical: canonicalUrl,
    },
  };
};

export const generateStaticParams = async () => {
  return [];
};

export default PaginatedInsightsAndResources;
