import { redirect } from 'next/navigation';
import { DriversPageQuery } from '@/lib/page-queries/page-queries/DriversPageQuery';
import { DriversPageClient } from './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('/404');
    case 'redirect':
      redirect(result.to);
    case 'error':
      // Pass error to client component
      return (
        <DriversPageClient
          pageDto={null}
          error={result.errorId}
        />
      );
    case 'ok':
      const pageDto = result.dto;
      const hasData = (pageDto.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={pageDto}
        />
      );
  }
}