import { redirect } from 'next/navigation';
import { routes } from '@/lib/routing/RouteConfig';
import { DriversPageQuery } from '@/lib/page-queries/page-queries/DriversPageQuery';
import { DriversPageClient } from '@/components/drivers/DriversPageClient';

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

  // Handle different result statuses
  switch (result.status) {
    case 'notFound':
      redirect(routes.error.notFound);
    case 'redirect':
      redirect(result.to);
    case 'error':
      // Pass error to client component
      return (
        <DriversPageClient
          pageDto={null}
          error={result.errorId}
        />
      );
    case 'ok':
      const viewModel = result.dto;
      const hasData = (viewModel.drivers?.length ?? 0) > 0;
      
      if (!hasData) {
        return (
          <DriversPageClient
            pageDto={null}
            empty={{
              title: 'No drivers found',
              description: 'There are no drivers in the system yet.',
            }}
          />
        );
      }

      return (
        <DriversPageClient
          pageDto={viewModel}
        />
      );
  }
}