import Footer from '@/components/layout/footer/Footer';
import '@/app/globals.css';
import 'leaflet/dist/leaflet.css';
import { notFound } from 'next/navigation';
import { NextIntlClientProvider } from 'next-intl';
import type { IPageParams } from '@/models/layout.d';
import Head from 'next/head';
import { getTranslations } from 'next-intl/server';
import { host } from '@/config';

export async function generateMetadata({ params }: { params: IPageParams }) {
  const { locale } = await params;
  const t = await getTranslations({ locale });
  const url = `${host}/${locale}`;

  return {
    title: {
      default: t('meta.defaultTitle'),
      template: t('meta.titleTemplate'),
    },
    description: t('meta.defaultDescription'),
    openGraph: {
      title: t('meta.defaultTitle'),
      description: t('meta.defaultDescription'),
      siteName: t('meta.siteName'),
      type: 'website',
      locale,
      url,
    },
    // authors: [{ name: t('meta.author') }],
  };
}

export default async function RootLayout({
  children,
  params,
}: Readonly<{
  children: React.ReactNode;
  params: IPageParams;
}>) {
  const paramsData = await params;
  const locale = paramsData.locale;

  const fontClass = locale === 'fa' ? 'font-fa' : 'font-en';

  let messages;
  try {
    messages = (await import(`../../../messages/${locale}.json`)).default;
  } catch (error) {
    notFound();
  }

  return (
    <html lang={locale} dir={locale === 'fa' ? 'rtl' : 'ltr'}>
      <Head>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        {/* <link rel="icon" href="/favicon.ico" /> */}
        <link
          rel="icon"
          type="image/png"
          href="/favicon/favicon-96x96.png"
          sizes="96x96"
        />
        <link rel="icon" type="image/svg+xml" href="/favicon/favicon.svg" />
        <link rel="shortcut icon" href="/favicon/favicon.ico" />
        <link
          rel="apple-touch-icon"
          sizes="180x180"
          href="/favicon/apple-touch-icon.png"
        />
        <meta name="apple-mobile-web-app-title" content="Pasargad Bricks" />
        <link rel="manifest" href="/favicon/site.webmanifest" />
      </Head>
      <body className={`min-h-svh w-full overflow-x-hidden ${fontClass}`}>
        <NextIntlClientProvider locale={locale} messages={messages}>
          {children}
          <Footer />
        </NextIntlClientProvider>
      </body>
    </html>
  );
}
