'use client';

import type { Metadata, Viewport } from 'next';
import NextError from 'next/error';
import { Geist, Geist_Mono } from 'next/font/google';

import '@/app/styles.css';

import { useEffect } from 'react';
import { env } from '@/env';
import { generateMetadata } from '@/lib/metadata';
import * as Sentry from '@sentry/nextjs';
import PlausibleProvider from 'next-plausible';

import { Button, ThemeProvider, Toaster } from '@spoon/ui';

export const metadata: Metadata = generateMetadata();

export const viewport: Viewport = {
  themeColor: [
    { media: '(prefers-color-scheme: light)', color: 'white' },
    { media: '(prefers-color-scheme: dark)', color: 'black' },
  ],
};

const geistSans = Geist({
  subsets: ['latin'],
  variable: '--font-geist-sans',
});
const geistMono = Geist_Mono({
  subsets: ['latin'],
  variable: '--font-geist-mono',
});

interface GlobalErrorProps {
  error: Error & { digest?: string };
  reset?: () => void;
}

const GlobalError = ({ error, reset = undefined }: GlobalErrorProps) => {
  useEffect(() => {
    Sentry.captureException(error);
  }, [error]);
  return (
    <PlausibleProvider
      domain={env.NEXT_PUBLIC_SITE_URL.trim().replace(/^https?:\/\//, '')}
      customDomain={env.NEXT_PUBLIC_PLAUSIBLE_URL}
    >
      <html lang='en' suppressHydrationWarning>
        <body
          className={`${geistSans.variable} ${geistMono.variable} antialiased`}
        >
          <ThemeProvider
            attribute='class'
            defaultTheme='system'
            enableSystem
            disableTransitionOnChange
          >
            <main className='flex min-h-screen flex-col items-center justify-center gap-4 p-6'>
              <NextError statusCode={0} />
              {reset !== undefined && (
                <Button onClick={() => reset()}>Try Again</Button>
              )}
              <Toaster />
            </main>
          </ThemeProvider>
        </body>
      </html>
    </PlausibleProvider>
  );
};
export default GlobalError;
