import { notFound } from 'next/navigation';
import { RacesTemplate } from '@/templates/RacesTemplate';
import { RacesPageQuery } from '@/lib/page-queries/races/RacesPageQuery';

export default async function Page() {
  const result = await RacesPageQuery.execute();
  
  if (result.isErr()) {
    const error = result.getError();
    
    switch (error) {
      case 'notFound':
        notFound();
      case 'redirect':
        // Would redirect to login or other page
        notFound();
      default:
        // For other errors, show error state in template
        return <RacesTemplate
          races={[]}
          totalCount={0}
          scheduledRaces={[]}
          runningRaces={[]}
          completedRaces={[]}
          isLoading={false}
          statusFilter="all"
          setStatusFilter={() => {}}
          leagueFilter="all"
          setLeagueFilter={() => {}}
          timeFilter="upcoming"
          setTimeFilter={() => {}}
          onRaceClick={() => {}}
          onLeagueClick={() => {}}
          onRegister={() => {}}
          onWithdraw={() => {}}
          onCancel={() => {}}
          showFilterModal={false}
          setShowFilterModal={() => {}}
          currentDriverId={undefined}
          userMemberships={[]}
        />;
    }
  }
  
  const viewData = result.unwrap();
  
  return <RacesTemplate
    races={viewData.races}
    totalCount={viewData.totalCount}
    scheduledRaces={viewData.scheduledRaces}
    runningRaces={viewData.runningRaces}
    completedRaces={viewData.completedRaces}
    isLoading={false}
    statusFilter="all"
    setStatusFilter={() => {}}
    leagueFilter="all"
    setLeagueFilter={() => {}}
    timeFilter="upcoming"
    setTimeFilter={() => {}}
    onRaceClick={() => {}}
    onLeagueClick={() => {}}
    onRegister={() => {}}
    onWithdraw={() => {}}
    onCancel={() => {}}
    showFilterModal={false}
    setShowFilterModal={() => {}}
    currentDriverId={undefined}
    userMemberships={[]}
  />;
}