import type { Metadata } from "next";
import { NextIntlClientProvider } from 'next-intl';
import { getMessages } from 'next-intl/server';
import { notFound } from 'next/navigation';
import { locales, type Locale } from '@/i18n/config';
import { ThemeProvider } from '@/contexts/ThemeContext';
import { Toaster } from 'sonner';
import "../globals.css";

export const metadata: Metadata = {
  title: {
    default: "ERP System - Enterprise Resource Planning",
    template: "%s | ERP System"
  },
  description: "Multi-tenant Enterprise Resource Planning System for SME businesses",
  robots: {
    index: false,
    follow: false,
    nocache: true,
    googleBot: {
      index: false,
      follow: false,
      'max-video-preview': -1,
      'max-image-preview': 'none',
      'max-snippet': -1,
    },
  },
  verification: {
    // Add site verification if needed
    // google: 'your-verification-code',
  },
  openGraph: {
    type: 'website',
    locale: 'ko_KR',
    siteName: 'ERP System',
    title: 'Enterprise Resource Planning System',
    description: 'Multi-tenant ERP System for SME businesses',
  },
  // Prevent caching of sensitive pages
  other: {
    'cache-control': 'no-cache, no-store, must-revalidate',
  },
};

export function generateStaticParams() {
  return locales.map((locale) => ({ locale }));
}

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

  // Ensure the incoming locale is valid
  if (!locales.includes(locale as Locale)) {
    notFound();
  }

  // Providing all messages to the client
  const messages = await getMessages();

  return (
    <html lang={locale} suppressHydrationWarning>
      <body className="antialiased">
        <ThemeProvider>
          <NextIntlClientProvider messages={messages}>
            {children}
            <Toaster richColors position="top-right" />
          </NextIntlClientProvider>
        </ThemeProvider>
      </body>
    </html>
  );
}
