import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { SiteFooter } from "@/components/site-footer";
import { checkExamGuard, PageType } from "@/components/exam/exam-guard";
import ExamOverview from "@/components/exam/exam-overview";
import { cachedGetExam } from "@/lib/api/exams";
import { getLocaleFromLangString } from "@/lib/locale";
import { getAlternateUrls } from "@/lib/seo";
import { t } from "@/locales";

export default async function ExamsOverviewPage(props: {
  params: Promise<{ lang?: string; exam_id: string }>;
}) {
  const params = await props.params;
  const { exam, examId, me, locale } = await checkExamGuard(
    params,
    PageType.ENTRY
  );

  return (
    <>
      <main className="mx-auto max-w-(--breakpoint-xl) p-4 md:p-6 lg:p-8 min-h-content">
        <ExamOverview examId={examId} exam={exam} user={me} />
      </main>
      <SiteFooter locale={locale} />
    </>
  );
}

export async function generateMetadata(props: {
  params: Promise<{ lang?: string; exam_id: string }>;
}): Promise<Metadata> {
  const params = await props.params;
  const locale = getLocaleFromLangString(params.lang);
  const examId = parseInt(params.exam_id, 10);
  const exam = await cachedGetExam(examId, locale);

  if (!exam) {
    notFound();
  }

  return {
    title: `${exam.title} | ${t(locale, "home.seo.title")}`,
    description: exam.description,
    keywords: `${exam.title}, ${t(locale, "home.seo.keywords")}`,
    openGraph: {
      title: `${exam.title} | ${t(locale, "home.seo.title")}`,
      description: exam.description,
      tags: t(locale, "home.seo.keywords"),
    },
    alternates: getAlternateUrls(locale, `/exams/${examId}`),
  };
}
