import { API_URL } from "@/lib/constants";
import HistoryPage from "@/page-components/public/history-page";
import React from "react";

type HistoryProps = {
  params: Promise<{
    slug: string;
  }>;
};

// generate static params
export const generateStaticParams = async () => {
  try {
    const response = await fetch(API_URL + "/histories");

    if (!response.ok) {
      console.error("Failed to fetch histories for static generation");
      return []; // Return empty array to prevent build failure
    }
    const histories = await response.json();

    return histories.data?.map((history: any) => {
      return {
        slug: history.slug,
      };
    });
  } catch (error) {
    console.error("\n\n Error in generateStaticParams:", error);
    return [];
  }
};

// generate metadata
export const generateMetadata = async ({ params }: HistoryProps) => {
  const { slug } = await params;
  try {
    const response = await fetch(API_URL + "/histories/" + slug);
    const history = await response.json();

    return {
      title: history?.title,
      description: history?.etaDescription,
      image: history?.imageUrl,
    };
  } catch {
    return {
      title: "history title",
    };
  }
};

const FetchSpecificPage = async ({ params }: HistoryProps) => {
  const { slug } = await params;
  return <HistoryPage slug={slug} />;
};

export default FetchSpecificPage;
