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

export default async function Page() {
  const result = await DriversPageQuery.execute();

  if (result.isErr()) {
    const error = result.getError();
    if (error === 'NotFound') {
      redirect(routes.error.notFound);
    }
    return (
      <DriversPageClient
        viewData={null}
        error={error}
      />
    );
  }

  const viewData = result.unwrap();
  return (
    <DriversPageClient
      viewData={viewData}
    />
  );
}