import type { ReactNode } from "react";
import { unstable_noStore as noStore } from "next/cache";
import { redirect } from "next/navigation";

import { SiteFooter } from "@/components/layout/site-footer";
import { SiteHeader } from "@/components/layout/site-header";
import { isAdminAuthenticated } from "@/lib/admin-auth";
import { getMaintenanceMode } from "@/lib/app-config";
import { getLocalizedPath, resolveLocale } from "@/lib/locale";

type SiteLayoutProps = {
  children: ReactNode;
  params: {
    locale: string;
  };
};

export const dynamic = "force-dynamic";
export const revalidate = 0;

export default async function SiteLayout({ children, params: { locale } }: SiteLayoutProps) {
  noStore();

  const localeKey = resolveLocale(locale);
  const maintenanceEnabled = await getMaintenanceMode();
  const authenticated = isAdminAuthenticated();

  if (maintenanceEnabled && !authenticated) {
    redirect(getLocalizedPath(localeKey, "/coming-soon"));
  }

  return (
    <div className="flex min-h-screen flex-col">
      <SiteHeader isAdmin={authenticated} />
      <main className="flex-1">{children}</main>
      <SiteFooter isAdmin={authenticated} />
    </div>
  );
}
