import { HomePageClient } from '@/client-wrapper/HomePageClient';
import { PageDataFetcher } from '@/lib/page/PageDataFetcher';
import { HomePageQuery } from '@/lib/page-queries/HomePageQuery';
import { notFound, redirect } from 'next/navigation';
import { routes } from '@/lib/routing/RouteConfig';
import { Metadata } from 'next';
import { MetadataHelper } from '@/lib/seo/MetadataHelper';
import { JsonLd } from '@/ui/JsonLd';

export const metadata: Metadata = MetadataHelper.generate({
  title: 'Professional iRacing League Management Platform',
  description: 'Experience the pinnacle of sim racing organization. GridPilot provides obsessive detail in race management, automated standings, and professional-grade tools for serious iRacing leagues.',
  path: '/',
});

export default async function Page() {
  if (await HomePageQuery.shouldRedirectToDashboard()) {
    redirect(routes.protected.dashboard);
  }

  const data = await PageDataFetcher.fetchManual(async () => {
    const result = await HomePageQuery.execute();
    return result.isOk() ? result.unwrap() : null;
  });
  
  if (!data) {
    notFound();
  }

  const jsonLd = {
    '@context': 'https://schema.org',
    '@type': 'WebSite',
    name: 'GridPilot',
    url: 'https://gridpilot.com',
    description: 'Professional iRacing League Management Platform',
    potentialAction: {
      '@type': 'SearchAction',
      target: 'https://gridpilot.com/search?q={search_term_string}',
      'query-input': 'required name=search_term_string',
    },
  };
  
  return (
    <>
      <JsonLd data={jsonLd} />
      <HomePageClient viewData={data} />
    </>
  );
}
