import { ClerkProvider } from '@clerk/nextjs';
import type { Metadata } from 'next';
import { headers } from 'next/headers';

import { AppProviders } from '@/app/(root)/AppProviders';
import { staticAssetUrl } from '@/utils/staticAssetUrl';

// NOTE: The embed page is using a different Tailwind CSS file that scans fewer files.
// Be sure to update `tailwind.embed.config.ts` and add any relevant components!
import EmbedClientLayout from './EmbedClientLayout';
import './embed.css';

export const fetchCache = 'force-no-store';

type Props = {
  params: Promise<{ slug: string }>;
};

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { slug } = await params;

  const headersList = await headers();
  const host = headersList.get('host') || 'suno.com';
  const canonicalUrl = `https://${host}/song/${slug}`;

  return {
    title: 'Suno',
    description: 'Suno',
    alternates: {
      canonical: canonicalUrl,
    },
  };
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <ClerkProvider>
      <html lang='en' style={{ height: '100%' }}>
        <head>
          <link
            rel='icon'
            type='image/x-icon'
            href='https://cdn-o.suno.com/favicon.ico'
            sizes='any'
          />
          <link
            rel='icon'
            type='image/png'
            href='https://cdn-o.suno.com/favicon-192x192.png'
            sizes='192x192'
          />
          <link
            rel='icon'
            type='image/png'
            href='https://cdn-o.suno.com/favicon-512x512.png'
            sizes='512x512'
          />
          {/* Critical font preloading - Other fonts load progressively via @font-face */}
          <link
            rel='preload'
            href={staticAssetUrl('PPNeueMontreal-Regular.woff')}
            as='font'
            type='font/woff'
            crossOrigin='anonymous'
            fetchPriority='high'
          />
          <link
            rel='preload'
            href={staticAssetUrl('PPNeueMontreal-Medium.woff')}
            as='font'
            type='font/woff'
            crossOrigin='anonymous'
          />
          <link
            rel='apple-touch-icon'
            href='https://cdn-o.suno.com/apple-touch-icon.png'
          />
        </head>
        <body style={{ height: '100%' }}>
          <AppProviders>
            <EmbedClientLayout>{children}</EmbedClientLayout>
          </AppProviders>
        </body>
      </html>
    </ClerkProvider>
  );
}
