import type { Metadata } from 'next'
import { Inter, Outfit } from 'next/font/google'
import './globals.css'
import { Providers } from './providers'
import { prisma } from '@innungsapp/shared'
import { getTenantSlug } from '@/lib/tenant'

const inter = Inter({ subsets: ['latin'], variable: '--font-inter' })
const outfit = Outfit({ subsets: ['latin'], variable: '--font-outfit' })

export async function generateMetadata(): Promise<Metadata> {
  const slug = await getTenantSlug()
  let org = null
  if (slug) {
    org = await prisma.organization.findUnique({
      where: { slug }
    })
  }

  const title = org ? `${org.name} | InnungsApp` : 'InnungsApp PRO | Die moderne Vereinssoftware fuer das Handwerk'
  const description = org
    ? `Willkommen im offiziellen Portal der ${org.name}.`
    : 'Digitale Mitgliederverwaltung, Push-News und Lehrlingsboerse fuer Handwerksinnungen.'
  const icon = org?.logoUrl || '/logo.png'

  return {
    title,
    description,
    icons: {
      icon: icon,
    },
    metadataBase: new URL('https://innungsapp.com'),
    openGraph: {
      title,
      description,
      url: 'https://innungsapp.com',
      siteName: 'InnungsApp PRO',
      locale: 'de_DE',
      type: 'website',
      images: [{ url: org?.logoUrl || '/mobile-mockup.png' }],
    },
  }
}

// Default export remains the component, but we remove the static metadata object below


export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="de">
      <body className={`${inter.variable} ${outfit.variable} font-sans bg-gray-50`}>
        <Providers>{children}</Providers>
      </body>
    </html>
  )
}
