import { NextIntlClientProvider } from "next-intl";
import { setRequestLocale } from "next-intl/server";
import React from "react";
import ConsentBanner from "../components/ConsentBanner";
import { getLocalizedMessage } from "@/lib/i18n-loader";

async function loadEnhancedMessages(locale: string) {
  // Lade basis JSON Messages
  const baseMessages = (await import(`../../messages/${locale}.json`)).default;
  
  // Erweitere mit Directus (wenn verfügbar)
  // Für jetzt: return base messages, Directus wird per Server Component geladen
  return baseMessages;
}

export default async function LocaleLayout({
  children,
  params,
}: {
  children: React.ReactNode;
  params: Promise<{ locale: string }>;
}) {
  const { locale } = await params;
  // Ensure next-intl actually uses the route segment locale for this request.
  setRequestLocale(locale);
  // Load messages explicitly by route locale to avoid falling back to the wrong
  // language when request-level locale detection is unavailable/misconfigured.
  const messages = await loadEnhancedMessages(locale);

  return (
    <NextIntlClientProvider locale={locale} messages={messages}>
      {children}
      <ConsentBanner />
    </NextIntlClientProvider>
  );
}

