import type { Metadata } from 'next';
import { headers } from 'next/headers';
import '@/styles/globals.css';
import Providers from './providers';

export const metadata: Metadata = {
  title: 'EDR Passenger Back-office',
  description: 'Ethio-Djibouti Railway Passenger Back-office',
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  // Nonce set per-request by middleware; required for this inline script under the CSP.
  const nonce = headers().get('x-nonce') ?? undefined;
  return (
    <html lang="en" suppressHydrationWarning>
      <head>
        <script
          nonce={nonce}
          dangerouslySetInnerHTML={{
            __html: `
              (function() {
                try {
                  const stored = localStorage.getItem('edr-theme');
                  const theme = stored ? JSON.parse(stored).state.isDark : window.matchMedia('(prefers-color-scheme: dark)').matches;
                  if (theme) document.documentElement.classList.add('dark');
                } catch (e) {}
              })();
            `,
          }}
        />
      </head>
      <body className="font-sans antialiased">
        <Providers>{children}</Providers>
      </body>
    </html>
  );
}
