"use client";

import { useEffect } from "react";
import Link from "next/link";
import { type Lang } from "@/lib/config";

export default function Error({
  error,
  reset,
  params,
}: {
  error: Error & { digest?: string };
  reset: () => void;
  params?: { lang?: Lang };
}) {
  const lang = params?.lang || "en";
  const isAr = lang === "ar";

  useEffect(() => {
    // Log the error to an error reporting service
    console.error("Error boundary caught:", error);
  }, [error]);

  return (
    <div className="flex min-h-screen flex-col items-center justify-center px-4">
      <div className="text-center space-y-6 max-w-md">
        <h1 className="text-6xl font-bold text-red-600">!</h1>
        <h2 className="text-2xl font-semibold text-neutral-700">
          {isAr ? "حدث خطأ ما" : "Something went wrong"}
        </h2>
        <p className="text-neutral-600">
          {isAr
            ? "عذراً، حدث خطأ غير متوقع. يرجى المحاولة مرة أخرى."
            : "Sorry, an unexpected error occurred. Please try again."}
        </p>
        <div className="flex gap-4 justify-center">
          <button
            onClick={reset}
            className="rounded-lg bg-brand-green px-6 py-3 text-white font-semibold hover:bg-brand-green/90 transition-colors"
          >
            {isAr ? "حاول مرة أخرى" : "Try Again"}
          </button>
          <Link
            href={`/${lang}`}
            className="rounded-lg border-2 border-neutral-200 px-6 py-3 font-semibold hover:border-brand-green transition-colors"
          >
            {isAr ? "العودة للرئيسية" : "Go Home"}
          </Link>
        </div>
      </div>
    </div>
  );
}
