import { PageWrapper } from '@/components/shared/state/PageWrapper';
import { PageDataFetcher } from '@/lib/page/PageDataFetcher';
import { TEAM_SERVICE_TOKEN } from '@/lib/di/tokens';
import { TeamService } from '@/lib/services/teams/TeamService';
import { Trophy } from 'lucide-react';
import { redirect } from 'next/navigation';
import type { TeamSummaryViewModel } from '@/lib/view-models/TeamSummaryViewModel';
import { TeamLeaderboardPageWrapper } from './TeamLeaderboardPageWrapper';

// ============================================================================
// MAIN PAGE COMPONENT
// ============================================================================

export default async function TeamLeaderboardPage() {
  // Fetch data using PageDataFetcher
  const teamsData = await PageDataFetcher.fetch<TeamService, 'getAllTeams'>(
    TEAM_SERVICE_TOKEN,
    'getAllTeams'
  );

  // Prepare data for template
  const data: TeamSummaryViewModel[] | null = teamsData;

  const hasData = (teamsData?.length ?? 0) > 0;

  // Handle loading state (should be fast since we're using async/await)
  const isLoading = false;
  const error = null;
  const retry = async () => {
    // In server components, we can't retry without a reload
    redirect('/teams/leaderboard');
  };

  return (
    <PageWrapper
      data={hasData ? data : null}
      isLoading={isLoading}
      error={error}
      retry={retry}
      Template={TeamLeaderboardPageWrapper}
      loading={{ variant: 'full-screen', message: 'Loading team leaderboard...' }}
      errorConfig={{ variant: 'full-screen' }}
      empty={{
        icon: Trophy,
        title: 'No teams found',
        description: 'There are no teams in the system yet.',
      }}
    />
  );
}