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

export const metadata: Metadata = MetadataHelper.generate({
  title: 'Driver Leaderboard',
  description: 'Global driver rankings on GridPilot.',
  path: '/leaderboards/drivers',
});

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} />;
}
