import type { Metadata } from 'next';
import './globals.css';
import { Toaster } from '@/components/ui/toaster';
import EnglishNumbersEnforcer from '@/components/layout/english-numbers-enforcer';
import PathnameTitleFallback from '@/components/layout/pathname-title-fallback';
import { getCurrentLocale } from '@/lib/i18n';

export async function generateMetadata(): Promise<Metadata> {
  try {
    const settings = await (await import('@/lib/postgres/data-access')).pgGetSettings() as Record<string, unknown>;
    const appName = String(settings.appName || 'StaffTrack Pro');
    return {
      title: {
        default: appName,
        template: `%s | ${appName}`,
      },
      description: 'An intelligent employee management system.',
    };
  } catch {
    return {
      title: {
        default: 'StaffTrack Pro',
        template: '%s | StaffTrack Pro',
      },
      description: 'An intelligent employee management system.',
    };
  }
}

export default async function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const locale = await getCurrentLocale();

  return (
    <html lang={locale} dir={locale === 'ar' ? 'rtl' : 'ltr'}>
      <head>
        <link rel="preconnect" href="https://fonts.googleapis.com" />
        <link
          rel="preconnect"
          href="https://fonts.gstatic.com"
          crossOrigin="anonymous"
        />
        <link
          href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;500;600;700&family=Inter:wght@400;500;600;700&display=swap"
          rel="stylesheet"
        />
      </head>
      <body className="font-body antialiased">
        <EnglishNumbersEnforcer />
        <PathnameTitleFallback />
        {children}
        <Toaster />
      </body>
    </html>
  );
}
