import type { Metadata } from "next";
import { getTranslations, setRequestLocale } from "next-intl/server";
import { getSettings } from "@/lib/settings";
import { parseLegalSections } from "@/lib/simpleMarkdown";
import { buildPageMetadata } from "@/lib/seo";
import { LegalDocument } from "@/components/LegalDocument";

export const dynamic = "force-dynamic";

export async function generateMetadata({
  params,
}: {
  params: Promise<{ locale: string }>;
}): Promise<Metadata> {
  const { locale } = await params;
  const t = await getTranslations({ locale, namespace: "legal" });
  return buildPageMetadata({
    locale,
    path: "/agb",
    title: t("termsTitle"),
    description: locale === "en" ? "Terms and conditions of Rakku Photography." : "Allgemeine Geschäftsbedingungen von Rakku Photography.",
  });
}

export default async function AgbPage({
  params,
}: {
  params: Promise<{ locale: string }>;
}) {
  const { locale } = await params;
  setRequestLocale(locale);
  const t = await getTranslations({ locale, namespace: "legal" });

  const settings = await getSettings();
  const raw =
    (locale === "en" && settings.legal?.termsTextEn) || settings.legal?.termsTextDe || "";
  const sections = parseLegalSections(raw);

  return (
    <LegalDocument
      eyebrow={t("termsEyebrow")}
      title={t("termsTitle")}
      updated={settings.legal?.termsUpdated}
      sections={sections}
      tocLabel={t("tocLabel")}
      seeAlsoLabel={t("seeAlsoLabel")}
      otherDocLabel={t("privacyTitle")}
      otherDocHref="/datenschutz"
    />
  );
}
