import { PageWrapper } from '@/components/shared/state/PageWrapper';
import { TeamService } from '@/lib/services/teams/TeamService';
import { Trophy } from 'lucide-react';
import { redirect } from 'next/navigation';
import { routes } from '@/lib/routing/RouteConfig';
import { TeamLeaderboardPageWrapper } from './TeamLeaderboardPageWrapper';

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

export default async function TeamLeaderboardPage() {
  // Manual wiring: create dependencies
  const service = new TeamService();

  // Fetch data through service
  const result = await service.getAllTeams();

  // Handle result
  let data = null;
  let error = null;
  
  if (result.isOk()) {
    data = result.unwrap();
  } else {
    const domainError = result.getError();
    error = new Error(domainError.message);
  }

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

  // Handle loading state (should be fast since we're using async/await)
  const isLoading = false;
  const retry = () => {
    // In server components, we can't retry without a reload
    redirect(routes.team.detail('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.',
      }}
    />
  );
}