import { notFound, redirect } from 'next/navigation';
import { DriverRankingsPageQuery } from '@/lib/page-queries/DriverRankingsPageQuery';
import { DriverRankingsPageClient } from './DriverRankingsPageClient';
import { routes } from '@/lib/routing/RouteConfig';

export default async function DriverLeaderboardPage() {
  const result = await DriverRankingsPageQuery.execute();
    
  if (result.isErr()) {
    const error = result.getError();
    
    // Handle different error types
    if (error === 'notFound') {
      notFound();
    } else if (error === 'redirect') {
      redirect(routes.public.home);
    } else {
      // serverError, networkError, unknown, validationError, unauthorized
      console.error('Driver rankings error:', error);
      notFound();
    }
  }
  
  // Success
  const viewData = result.unwrap();
  return <DriverRankingsPageClient viewData={viewData} />;
}
