import { notFound } from 'next/navigation';
import { PageWrapper } from '@/components/shared/state/PageWrapper';
import { RaceResultsPageQuery } from '@/lib/page-queries/races/RaceResultsPageQuery';
import { RaceResultsPageClient } from '@/client-wrapper/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 PageWrapper handle it
    return (
      <PageWrapper
        data={undefined}
        Template={RaceResultsPageClient}
        error={new globalThis.Error('Failed to load race results')}
      />
    );
  }
  
  const viewData = result.unwrap();

  return (
    <PageWrapper
      data={viewData}
      Template={RaceResultsPageClient}
    />
  );
}
