import { notFound } from 'next/navigation';
import { PageWrapper } from '@/ui/PageWrapper';
import { RaceDetailPageQuery } from '@/lib/page-queries/races/RaceDetailPageQuery';
import { RaceDetailPageClient } from '@/client-wrapper/RaceDetailPageClient';

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

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}
    />
  );
}