import { getStorefrontTheme } from '@/lib/db/queries/getStorefrontTheme';
import { buildThemeCss } from '@/lib/theme/storefrontTheme';
import { themeFontVariables } from '@/lib/theme/fonts';

interface StorefrontThemeRootProps {
  children: React.ReactNode;
  lang: string;
  dir: 'ltr' | 'rtl';
  // The cookie banner is in use — shows the footers' "Cookie settings" link.
  cookieConsent?: boolean;
}

// Root element of every storefront route group. Server-rendered, so the
// admin's theme is in the very first HTML byte — no flash of the default
// colors and no client JS. The CSS only ever contains validated hex colors
// and whitelisted font/radius keys (normalizeTheme), never raw DB text.
export default async function StorefrontThemeRoot({ children, lang, dir, cookieConsent = false }: StorefrontThemeRootProps) {
  const theme = await getStorefrontTheme();

  return (
    <div data-storefront data-cookie-consent={cookieConsent ? 'on' : undefined} lang={lang} dir={dir} className={`${themeFontVariables} min-h-screen`}>
      <style dangerouslySetInnerHTML={{ __html: buildThemeCss(theme, '[data-storefront]') }} />
      {children}
    </div>
  );
}
