'use client';

import React from 'react';
import { useRouter } from 'next/navigation';
import { routes } from '@/lib/routing/RouteConfig';
import { ServerErrorTemplate, type ServerErrorViewData } from '@/templates/ServerErrorTemplate';
import { ClientWrapperProps } from '@/lib/contracts/components/ComponentContracts';

/**
 * ServerErrorPageClient
 * 
 * Client-side entry point for the 500 page.
 * Manages navigation and retry logic and wires it to the template.
 */
export function ServerErrorPageClient({ viewData: initialViewData }: Partial<ClientWrapperProps<ServerErrorViewData>>) {
  const router = useRouter();
  
  const handleHome = () => {
    router.push(routes.public.home);
  };

  const handleRetry = () => {
    window.location.reload();
  };

  const error = new Error('Internal Server Error') as Error & { digest?: string };
  error.digest = 'HTTP_500';

  const viewData: ServerErrorViewData = initialViewData ?? {
    error,
    incidentId: error.digest
  };

  return (
    <ServerErrorTemplate 
      viewData={viewData} 
      onRetry={handleRetry} 
      onHome={handleHome} 
    />
  );
}
