'use client';

import { useEffect } from 'react';
import { useTranslations } from 'next-intl';
import { getAppServices } from '@/lib/services/create-services';
import { Container, Button, Heading } from '@/components/ui';
import Scribble from '@/components/Scribble';

export default function Error({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  const t = useTranslations('Error');

  useEffect(() => {
    // Treat "Failed to find Server Action" as a deployment sync issue and reload
    if (error?.message?.includes('Failed to find Server Action')) {
      window.location.reload();
      return;
    }

    const services = getAppServices();
    services.errors.captureException(error);
    services.logger.error('Application error caught by boundary', {
      message: error?.message || 'Unknown error',
      stack: error?.stack,
      digest: error?.digest,
    });
  }, [error]);

  return (
    <Container className="relative py-24 flex flex-col items-center justify-center text-center min-h-[70vh] overflow-hidden">
      {/* Industrial Background Element */}
      <div className="absolute inset-0 -z-10 opacity-[0.03] pointer-events-none flex items-center justify-center">
        <span className="text-[20rem] font-bold select-none">500</span>
      </div>

      <div className="relative mb-8">
        <Heading level={1} className="text-6xl md:text-8xl font-bold mb-2 text-saturated">
          500
        </Heading>
        <Scribble variant="underline" className="w-full h-6 -bottom-2 left-0 text-saturated/40" />
      </div>

      <Heading level={2} className="text-2xl md:text-3xl font-bold mb-4">
        {t('title')}
      </Heading>

      <p className="text-white/60 mb-10 max-w-md text-lg">{t('description')}</p>

      <div className="flex flex-col sm:flex-row gap-4">
        <Button onClick={() => reset()} variant="saturated" size="lg">
          {t('tryAgain')}
        </Button>
        <Button href="/" variant="outline" size="lg">
          {t('goHome')}
        </Button>
      </div>

      {/* Decorative Industrial Line */}
      <div className="absolute top-0 left-1/2 -translate-x-1/2 w-px h-24 bg-gradient-to-b from-saturated/50 to-transparent" />
    </Container>
  );
}
