"use client";

import { useEffect } from "react";

// Last-resort boundary: catches errors thrown in the root layout itself. It
// replaces the whole document, so it must render its own <html>/<body> and can't
// rely on the app's global CSS — hence inline styles.
export default function GlobalError({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  useEffect(() => {
    console.error("Global error boundary caught:", error);
  }, [error]);

  return (
    <html lang="en">
      <body
        style={{
          margin: 0,
          minHeight: "100vh",
          display: "flex",
          flexDirection: "column",
          alignItems: "center",
          justifyContent: "center",
          gap: "1rem",
          padding: "2rem",
          background: "#0a0a0a",
          color: "#ededed",
          fontFamily:
            "ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif",
          textAlign: "center",
        }}
      >
        <h1 style={{ fontSize: "1.75rem", fontWeight: 800, margin: 0 }}>
          Something went wrong
        </h1>
        <p style={{ color: "#a1a1aa", maxWidth: "28rem", margin: 0 }}>
          The site hit an unexpected error. Please try again in a moment.
        </p>
        <button
          onClick={() => reset()}
          style={{
            marginTop: "0.5rem",
            padding: "0.75rem 2rem",
            borderRadius: "0.5rem",
            border: "none",
            fontWeight: 700,
            cursor: "pointer",
            background: "#f7931a",
            color: "#0a0a0a",
          }}
        >
          Try again
        </button>
        {error?.digest && (
          <p
            style={{
              color: "#71717a",
              fontSize: "0.75rem",
              fontFamily: "ui-monospace, monospace",
            }}
          >
            ref: {error.digest}
          </p>
        )}
      </body>
    </html>
  );
}
