"use client";

import { useEffect } from "react";

/**
 * Last-resort boundary: catches throws from the root layout itself, which the
 * per-route `error.tsx` cannot reach. It replaces the whole document, so it has
 * to render its own `<html>`/`<body>` and cannot rely on the app's fonts,
 * providers, or global CSS — hence the inline styles.
 */
export default function GlobalError({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  useEffect(() => {
    console.error(error);
  }, [error]);

  return (
    <html lang="en">
      <body
        style={{
          margin: 0,
          minHeight: "100dvh",
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          backgroundColor: "#f9fafb",
          fontFamily: "system-ui, -apple-system, sans-serif",
          padding: "1rem",
        }}
      >
        <div style={{ textAlign: "center", maxWidth: "28rem" }}>
          <h1 style={{ fontSize: "1.5rem", fontWeight: 800, color: "#111827", marginBottom: "0.75rem" }}>
            Something went wrong
          </h1>
          <p style={{ color: "#6b7280", marginBottom: "2rem", lineHeight: 1.6 }}>
            Biznie ran into an unexpected error. Please reload the page.
          </p>
          {error.digest && (
            <p style={{ color: "#9ca3af", fontSize: "0.75rem", fontFamily: "monospace", marginBottom: "1.5rem" }}>
              Reference: {error.digest}
            </p>
          )}
          <button
            onClick={reset}
            style={{
              backgroundColor: "#CC1414",
              color: "#fff",
              border: "none",
              borderRadius: "0.75rem",
              padding: "0.75rem 1.5rem",
              fontSize: "0.875rem",
              fontWeight: 700,
              cursor: "pointer",
            }}
          >
            Try Again
          </button>
        </div>
      </body>
    </html>
  );
}
