import type { Metadata, Viewport } from 'next';
import './globals.css';
import { SiteFooter, SiteHeader } from '@/components/Chrome';
import { JsonLd } from '@/components/JsonLd';
import { CompareBar } from '@/components/AccountWidgets';
import { ScrollToTop } from '@/components/ScrollToTop';
import { OG_IMAGE, SITE, abs } from '@/lib/site';

export const metadata: Metadata = {
  metadataBase: new URL(SITE.url),
  title: { default: `${SITE.name} — ${SITE.tagline}`, template: `%s | ${SITE.name}` },
  description: SITE.description,
  applicationName: SITE.name,
  keywords: ['Justice Choice', 'find a lawyer', 'compare lawyers', 'lawyer rankings', 'verified lawyers', 'lawyer directory'],
  authors: [{ name: SITE.name, url: SITE.url }],
  creator: SITE.name,
  publisher: SITE.name,
  formatDetection: { telephone: false },
  // No url here: each page sets its own og:url, and none should inherit the homepage's
  openGraph: { type: 'website', siteName: SITE.name, locale: 'en_US', title: `${SITE.name} — ${SITE.tagline}`, description: SITE.description, images: [OG_IMAGE] },
  twitter: { card: 'summary_large_image', title: `${SITE.name} — ${SITE.tagline}`, description: SITE.description, images: [OG_IMAGE.url] },
  robots: { index: true, follow: true, googleBot: { index: true, follow: true, 'max-image-preview': 'large', 'max-snippet': -1 } },
};

export const viewport: Viewport = {
  themeColor: [
    { media: '(prefers-color-scheme: light)', color: '#f5f7f4' },
    { media: '(prefers-color-scheme: dark)', color: '#12181b' },
  ],
};

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body className="min-h-screen">
        <a href="#main" className="sr-only focus:not-sr-only focus:fixed focus:left-4 focus:top-4 focus:z-50 focus:rounded-lg focus:bg-surface focus:px-4 focus:py-2 focus:shadow-lg">
          Skip to content
        </a>
        <SiteHeader />
        <main id="main">{children}</main>
        <SiteFooter />
        <CompareBar />
        <ScrollToTop />
        <JsonLd
          data={{
            '@context': 'https://schema.org',
            '@type': 'Organization',
            '@id': `${SITE.url}/#organization`,
            name: SITE.name,
            url: SITE.url,
            logo: abs('/icon.svg'),
            description: SITE.description,
          }}
        />
      </body>
    </html>
  );
}
