import type { Metadata } from "next";
import { hasLocale, NextIntlClientProvider } from "next-intl";
import { getTranslations, setRequestLocale } from "next-intl/server";
import { notFound } from "next/navigation";
import { AppProviders } from "@/components/app-providers";
import { SiteFooter } from "@/components/site-footer";
import { SiteHeader } from "@/components/site-header";
import { routing } from "@/i18n/routing";

type LocaleLayoutProps = {
  children: React.ReactNode;
  params: Promise<{ locale: string }>;
};

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

export async function generateMetadata({
  params,
}: LocaleLayoutProps): Promise<Metadata> {
  const { locale } = await params;
  const t = await getTranslations({ locale, namespace: "Metadata" });

  return {
    title: t("title"),
    description: t("description"),
    alternates: {
      canonical: `/${locale}`,
      languages: {
        en: "/en",
        el: "/el",
      },
    },
  };
}

export default async function LocaleLayout({
  children,
  params,
}: LocaleLayoutProps) {
  const { locale } = await params;

  if (!hasLocale(routing.locales, locale)) {
    notFound();
  }

  setRequestLocale(locale);

  const navigation = await getTranslations({
    locale,
    namespace: "Navigation",
  });

  const footer = await getTranslations({
    locale,
    namespace: "Footer",
  });

  const navLabels = {
    accommodation: navigation("accommodation"),
    gallery: navigation("gallery"),
    amenities: navigation("amenities"),
    location: navigation("location"),
    discoverParos: navigation("discoverParos"),
    booking: navigation("booking"),
    faq: navigation("faq"),
    contact: navigation("contact"),
    language: navigation("language"),
  };

  const footerLabels = {
    footerDescription: footer("description"),
    registration: footer("registration"),
    "house-rules": footer("houseRules"),
    "cancellation-policy": footer("cancellationPolicy"),
    "privacy-policy": footer("privacyPolicy"),
    "cookie-policy": footer("cookiePolicy"),
    "terms-booking-conditions": footer("terms"),
  };

  return (
    <NextIntlClientProvider>
      <AppProviders>
        <div className="flex min-h-screen flex-col">
          <SiteHeader labels={navLabels} />
          <main className="flex-1">{children}</main>
          <SiteFooter labels={footerLabels} />
        </div>
      </AppProviders>
    </NextIntlClientProvider>
  );
}
