import type { Metadata, Viewport } from "next";
import { Toaster } from "sonner";

import { ReduxProviderWrapper } from "./ReduxProvider";
import SiteFooter from "@/components/layout/SiteFooter";
import SiteHeader from "@/components/layout/SiteHeader";
import { env } from "@/lib/env";
import { fontVariables } from "@/lib/fonts";

import "./globals.css";

export const metadata: Metadata = {
  metadataBase: new URL(env.appUrl),
  title: {
    default: "Business mode · leisure.com",
    template: "%s · Business mode",
  },
  description:
    "Employees request work flights and hotels, one admin approves them against travel policy, " +
    "and approved trips are booked automatically on the company card.",
  icons: {
    icon: "/images/v3/logo.svg",
  },
};

/**
 * Explicit `<meta name="color-scheme">` on top of globals.css's own
 * `color-scheme: light` rule. The CSS property alone styles native form
 * controls/scrollbars, but some browsers' automatic dark-mode repainting
 * (e.g. Chrome's force-dark on a system/browser set to dark) still inverts
 * page colors unless this meta tag is also present — this app intentionally
 * doesn't support dark mode anywhere, so every page must opt out of that.
 */
export const viewport: Viewport = {
  colorScheme: "light",
};

/**
 * bootstrap-icons.css and biz-ui.css/biz-payment.css, site-wide.
 *
 * All three were originally only under app/admin/layout.tsx — reasonable
 * when every `.bz-*`/`bi-*` consumer was an admin page (Payment methods,
 * Checkout), wrong once LEIS-951's Employee Profile page (not under
 * /admin) needed them too. Moved here so the next non-admin Business page
 * that reaches for this design system doesn't hit the same silent
 * unstyled-page gap.
 *
 * All three stay plain blocking `<link>` tags, not NonBlockingStylesheet:
 * they're self-hosted/same-origin (public/assets/css/...), so there's no
 * slow third-party CDN hop to defer around — blocking on a fast same-origin
 * stylesheet is the normal, correct choice (this is exactly the tradeoff
 * app/admin/layout.tsx's own history already worked through for
 * bootstrap-icons.css specifically: deferring it meant every icon on the
 * page was invisible for ~1.3s on each load, which is worse than blocking
 * for a few same-origin milliseconds).
 */
export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en" className={fontVariables} suppressHydrationWarning>
      <body className="bg-lx-body antialiased">
        {/* eslint-disable-next-line @next/next/no-css-tags */}
        <link rel="stylesheet" href="/assets/css/bootstrap-icons.css" />
        {/* eslint-disable-next-line @next/next/no-css-tags */}
        <link rel="stylesheet" href="/assets/css/biz-ui.css" />
        {/* eslint-disable-next-line @next/next/no-css-tags */}
        <link rel="stylesheet" href="/assets/css/biz-payment.css" />
        {/*
         * `.lx-chrome-offset` replaces leisure-ui's `.wrapper` padding-top: the
         * header is fixed, so content needs 47/50/62px of clearance depending
         * on which header is showing.
         */}
        <ReduxProviderWrapper>
          <div className="lx-chrome-offset flex min-h-dvh flex-col">
            <SiteHeader />
            <main className="flex-1">{children}</main>
            <SiteFooter />
          </div>
        </ReduxProviderWrapper>
        {/*
          Matched to leisure-ui's, so a toast reads the same on both sides of
          the product — top-right, rich colours, a close button, four seconds.
          The offsets are corp's own: the chrome is fixed, and a toast pinned
          to `top: 0` lands underneath it. The header is 47px on a phone, 50px
          from 768px and 62px from 1226px (see `.lx-chrome-offset` in
          globals.css), so these clear the tallest of those plus a margin
          rather than tracking each breakpoint — a toast sitting slightly low
          is fine; one hidden behind the header is not.
        */}
        <Toaster
          position="top-right"
          offset={{ top: "78px", right: "16px" }}
          mobileOffset={{ top: "60px", right: "12px", left: "12px" }}
          richColors
          closeButton
          duration={4000}
        />
      </body>
    </html>
  );
}
