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: 'iRacing League Management Infrastructure',
  description: 'Infrastructure for iRacing league management. Automated standings, race management, and stewarding tools.',
  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} />
    </>
  );
}
