import { Metadata } from "next";
import AcademyProfileClient from "./AcademyProfileClient";
import { generatePageMetadata } from "@/utils/generatePageMetadata";
import { getApiBaseUrl } from "@/components/main/BaseUrl";
import { getServerLanguage } from "@/lib/i18n/server";
import { translateCommon } from "@/lib/i18n/translate";

type Props = {
  params: Promise<{ academyId: string }>;
};

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { academyId } = await params;

  try {
    const res = await fetch(`${getApiBaseUrl()}/academy/findAcademyById`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ _id: academyId }),
      cache: "no-store",
    });
    const data = await res.json();
    const academy = data?.academy;

    if (academy?.academy_name) {
      const lang = await getServerLanguage();
      return generatePageMetadata({
        title: academy.academy_name,
        description: academy.bio || academy.academy_name,
        path: `/academy/profile/${academyId}`,
        appendSiteName: true,
        lang,
      });
    }
  } catch {
    // fallback below
  }

  const lang = await getServerLanguage();

  return generatePageMetadata({
    title: translateCommon(lang, "academy.course.profileMetaTitle"),
    description: translateCommon(lang, "academy.course.profileMetaDescription"),
    path: `/academy/profile/${academyId}`,
    lang,
  });
}

export default function AcademyProfilePage() {
  return <AcademyProfileClient />;
}
