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

export const metadata: Metadata = MetadataHelper.generate({
  title: 'Team Leaderboard',
  description: 'Global team rankings on GridPilot. See the top performing sim racing teams and their competitive statistics.',
  path: '/leaderboards/teams',
});

export default async function TeamLeaderboardPage() {
  const result = await TeamRankingsPageQuery.execute();
    
  if (result.isErr()) {
    const error = result.getError();
    
    if (error === 'notFound') {
      notFound();
    } else if (error === 'redirect') {
      redirect(routes.public.home);
    } else {
      logger.error('Team rankings error:', undefined, { error });
      notFound();
    }
  }
  
  const viewData = result.unwrap();
  return <TeamRankingsPageClient viewData={viewData} />;
}
