import { notFound } from 'next/navigation';
import { PageWrapper } from '@/components/shared/state/PageWrapper';
import { RaceDetailPageQuery } from '@/lib/page-queries/races/RaceDetailPageQuery';
import { RaceDetailPageClient } from '@/client-wrapper/RaceDetailPageClient';
import { Metadata } from 'next';
import { MetadataHelper } from '@/lib/seo/MetadataHelper';

interface RaceDetailPageProps {
  params: Promise<{
    id: string;
  }>;
}

export async function generateMetadata({ params }: RaceDetailPageProps): Promise<Metadata> {
  const { id: raceId } = await params;
  const result = await RaceDetailPageQuery.execute({ raceId, driverId: '' });

  if (result.isErr()) {
    return MetadataHelper.generate({
      title: 'Race Not Found',
      description: 'The requested race details could not be found on GridPilot.',
      path: `/races/${raceId}`,
    });
  }

  const viewData = result.unwrap();
  const race = viewData.race;
  const leagueName = viewData.league?.name;
  const title = leagueName ? `${race.track} - ${leagueName}` : `${race.track} - ${race.car}`;

  return MetadataHelper.generate({
    title: `${title} | Race Results`,
    description: `Detailed race results, standings, and session reports for the ${race.car} race at ${race.track}${leagueName ? ` in ${leagueName}` : ''} on GridPilot. Professional iRacing event coverage with obsessive detail.`,
    path: `/races/${raceId}`,
  });
}

export default async function RaceDetailPage({ params }: RaceDetailPageProps) {
  const { id: raceId } = await params;
  
  if (!raceId) {
    notFound();
  }

  // Execute PageQuery
  const result = await RaceDetailPageQuery.execute({ raceId, driverId: '' });
  
  if (result.isErr()) {
    const error = result.getError();
    
    if (error === 'notFound') {
      notFound();
    }
    // For other errors, let PageWrapper handle it
    return (
      <PageWrapper
        data={undefined}
        Template={RaceDetailPageClient}
        error={new globalThis.Error('Failed to load race details')}
      />
    );
  }
  
  const viewData = result.unwrap();
  
  return (
    <PageWrapper
      data={viewData}
      Template={RaceDetailPageClient}
    />
  );
}