import { redirect } from 'next/navigation';
import { DriverRankingsPageQuery } from '@/lib/page-queries/page-queries/DriverRankingsPageQuery';
import { DriverRankingsViewDataBuilder } from '@/lib/builders/view-data/DriverRankingsViewDataBuilder';
import { DriverRankingsTemplate } from '@/templates/DriverRankingsTemplate';

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

export default async function DriverLeaderboardPage() {
  // Execute the page query
  const result = await DriverRankingsPageQuery.execute();

  // Handle different result statuses
  switch (result.status) {
    case 'notFound':
      redirect('/404');
    case 'redirect':
      redirect(result.to);
    case 'error':
      // For now, show empty state. In a real app, you'd pass error to client
      return (
        <div className="max-w-7xl mx-auto px-4 py-12 text-center">
          <div className="text-red-400 mb-4">Error loading driver rankings</div>
          <p className="text-gray-400">Please try again later</p>
        </div>
      );
    case 'ok':
      const viewData = DriverRankingsViewDataBuilder.build(result.dto);
      const hasData = (viewData.drivers?.length ?? 0) > 0;
      
      if (!hasData) {
        return (
          <DriverRankingsTemplate
            viewData={{
              drivers: [],
              podium: [],
              searchQuery: '',
              selectedSkill: 'all',
              sortBy: 'rank',
              showFilters: false,
            }}
          />
        );
      }

      return (
        <DriverRankingsTemplate viewData={viewData} />
      );
  }
}