import { CustomPage } from '@/components/page/custom-page';
import { pagesSource } from '@/lib/docs/source';
import { constructMetadata } from '@/lib/metadata';
import { getUrlWithLocale } from '@/lib/urls/urls';
import type { NextPageProps } from '@/types/next-page-props';
import type { Metadata } from 'next';
import type { Locale } from 'next-intl';
import { getTranslations } from 'next-intl/server';
import { notFound } from 'next/navigation';

export async function generateMetadata({
  params,
}: {
  params: Promise<{ locale: Locale }>;
}): Promise<Metadata | undefined> {
  const { locale } = await params;
  const page = pagesSource.getPage(['cookie-policy'], locale);

  if (!page) {
    console.warn(
      `generateMetadata, page not found for cookie-policy, locale: ${locale}`
    );
    return {};
  }

  const t = await getTranslations({ locale, namespace: 'Metadata' });

  return constructMetadata({
    title: page.data.title + ' | ' + t('title'),
    description: page.data.description,
    canonicalUrl: getUrlWithLocale('/cookie', locale),
  });
}

export default async function CookiePolicyPage(props: NextPageProps) {
  const params = await props.params;
  if (!params) {
    notFound();
  }

  const locale = params.locale as string;
  const page = pagesSource.getPage(['cookie-policy'], locale);

  if (!page) {
    notFound();
  }

  return <CustomPage page={page} />;
}
