"use client";

import { useEffect } from "react";
import Link from "next/link";

// Route-segment error boundary. Catches runtime errors thrown while rendering any
// page/segment (e.g. a request-time fetch or a bad build artifact) and shows a
// recoverable UI with a retry, instead of a bare 500.
export default function Error({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  useEffect(() => {
    console.error("Route error boundary caught:", error);
  }, [error]);

  return (
    <div className="min-h-screen flex flex-col items-center justify-center px-8">
      <span className="text-7xl md:text-9xl font-black tracking-tighter text-transparent bg-clip-text bg-gradient-to-r from-primary to-primary-container leading-none">
        Oops
      </span>
      <h1 className="text-2xl md:text-3xl font-bold mt-6 mb-3">
        Something went wrong
      </h1>
      <p className="text-on-surface-variant mb-10 text-center max-w-md">
        This page hit an unexpected error. It&apos;s usually temporary — try again
        in a moment.
      </p>
      <div className="flex flex-wrap gap-3 justify-center">
        <button
          onClick={() => reset()}
          className="bg-gradient-to-r from-primary to-primary-container text-on-primary px-8 py-3 rounded-lg font-bold hover:scale-105 transition-transform"
        >
          Try again
        </button>
        <Link
          href="/"
          className="border border-zinc-700 px-8 py-3 rounded-lg font-bold hover:bg-zinc-800/50 transition-colors"
        >
          Back to Home
        </Link>
      </div>
      {error?.digest && (
        <p className="text-on-surface-variant/50 text-xs mt-8 font-mono">
          ref: {error.digest}
        </p>
      )}
    </div>
  );
}
