import type { Metadata } from 'next';
import './globals.css';
import { AuthProvider } from '@/lib/auth-context';
import { QueryProvider } from '@/lib/query';
import { ThemeProvider } from '@/lib/theme';
import { DialogProvider } from '@/components/dialog';
import { RightClickGuard } from '@/components/right-click';

export const metadata: Metadata = {
  title: 'Qtrial — Streamline Quality. Ensure Compliance.',
  description: 'Ensure Compliance workflow, managing deviations, CAPAs, QC sample testing, escalations, and audit-ready reporting.',
  icons: { icon: '/logo.svg' },
};

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <head>
        {/* Apply saved theme before paint to avoid a light-mode flash */}
        <script
          dangerouslySetInnerHTML={{
            __html: `(function(){try{var t=localStorage.getItem('pharma-theme');if(t==='dark'||(!t&&matchMedia('(prefers-color-scheme: dark)').matches)){document.documentElement.classList.add('dark')}}catch(e){}})();`,
          }}
        />
      </head>
      <body>
        <RightClickGuard />
        <ThemeProvider>
          <QueryProvider>
            <AuthProvider><DialogProvider>{children}</DialogProvider></AuthProvider>
          </QueryProvider>
        </ThemeProvider>
      </body>
    </html>
  );
}
