import { localizedMetadata } from "@/lib/i18n/serverMetadata";
import type { Metadata } from "next";
import { Suspense } from "react";

import RedirectIfLoggedIn from "@/components/shared/RedirectIfLoggedIn";
import ScrollToTop from "@/components/shared/ScrollToTop";
import Header from "@/components/layout/Header";
import FooterSection from "@/components/layout/FooterSection";

/**
 * Sign-in, sign-up and account-recovery screens are kept out of search
 * results.
 *
 * Applied at the layout rather than per page for two reasons: it covers all
 * eight routes in this group at once, and three of them (`login`,
 * `reset-password`, `email-verification`) are Client Components, which cannot
 * export `metadata` at all. Titles stay per-page where a page sets one.
 */
const pageMetadata: Metadata = {
  robots: { index: false, follow: false },
};

export async function generateMetadata(): Promise<Metadata> {
  return localizedMetadata(pageMetadata);
}

export default function AuthLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <RedirectIfLoggedIn>
      <div className="min-h-screen flex flex-col">
        <Header />
        <main className="main-section flex-1">
          <ScrollToTop />
          {children}
        </main>
        {/* Behind a boundary, as in `(main)/layout.tsx`: this layout used to
            await the CMS read before rendering the sign-in form. A reader
            trying to log in should not wait on the footer's link list. */}
        <Suspense fallback={null}>
          <FooterSection />
        </Suspense>
      </div>
    </RedirectIfLoggedIn>
  );
}
