import { redirect } from 'next/navigation';
import { routes } from '@/lib/routing/RouteConfig';
import { DriversPageQuery } from '@/lib/page-queries/DriversPageQuery';
import { DriversPageClient } from '@/client-wrapper/DriversPageClient';
import { Metadata } from 'next';
import { MetadataHelper } from '@/lib/seo/MetadataHelper';

export const metadata: Metadata = MetadataHelper.generate({
  title: 'Sim Racing Drivers',
  description: 'Explore the elite roster of sim racing drivers on GridPilot. Detailed performance metrics, career history, and professional driver profiles for the iRacing community.',
  path: '/drivers',
});

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}
    />
  );
}