import { notFound, redirect } from 'next/navigation';
import { DriverRankingsPageQuery } from '@/lib/page-queries/DriverRankingsPageQuery';
import { DriverRankingsPageClient } from '@/client-wrapper/DriverRankingsPageClient';
import { routes } from '@/lib/routing/RouteConfig';
import { logger } from '@/lib/infrastructure/logging/logger';

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
      logger.error('Driver rankings error:', undefined, { error });
      notFound();
    }
  }
  
  // Success
  const viewData = result.unwrap();
  return <DriverRankingsPageClient viewData={viewData} />;
}
