import { appConfig } from "@/config/app"
import DetailPostKuisineContainer from "./section/DetailPostKuisineContainer"
import { Metadata } from "next"
import { notFound } from "next/navigation"
import { revalidatePath } from "next/cache"
import { cookies } from "next/headers"

type Props = { params: { postid: string, cateid: string } }

export const metadata: Metadata = {
  title: "Kuisine",
}

const fetchKuisineDetail: any = async (id: string, accessToken: string) => {
  try {
    const res = await fetch(`${appConfig.apiUrl}/kuisines/${id}`, {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        Origin: appConfig.appUrl,
        Authorization: "Bearer " + accessToken,
      },
      cache: "no-store",
    })
    if (!res.ok) throw new Error("Failed to fetch kuisine data")
    return res.json()
  } catch (error) {
    console.error("Error fetching kuisine data:", error)
    return null
  }
}

const revalidate=async ( id: number ) => {
  'use server';
  revalidatePath( `${appConfig.apiUrl}/kuisines/${id}` );
};

export default async function DetailBookPage(props: Props) {
  const accessToken = cookies().get("access_token")?.value
  const { postid } = props.params

  const kuisine = await fetchKuisineDetail(postid, accessToken)

  if (!kuisine) notFound()
  return <DetailPostKuisineContainer kuisineDetail={kuisine?.data || {}} revalidate={revalidate} />
}
