import { notFound } from 'next/navigation';
import { StatefulPageWrapper } from '@/components/shared/state/StatefulPageWrapper';
import { RaceResultsPageQuery } from '@/lib/page-queries/races/RaceResultsPageQuery';
import RaceResultsPageClient from './RaceResultsPageClient';

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

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

  // Execute PageQuery
  const result = await RaceResultsPageQuery.execute({ raceId });
  
  if (result.isErr()) {
    const error = result.getError();
    
    if (error === 'notFound') {
      notFound();
    }
    // For other errors, let StatefulPageWrapper handle it
    return (
      <StatefulPageWrapper
        data={undefined}
        Template={RaceResultsPageClient as any}
        error={new Error('Failed to load race results')}
        retry={() => Promise.resolve()}
      />
    );
  }
  
  const viewData = result.unwrap();

  return (
    <StatefulPageWrapper
      data={viewData}
      Template={RaceResultsPageClient}
      retry={() => Promise.resolve()}
    />
  );
}
