"use client";

import { useEffect } from "react";

/**
 * Root-level error boundary. Catches failures thrown from the root layout
 * (e.g. a transient backend outage while fetching header/footer data) and
 * shows a branded "try again" screen instead of a raw 500.
 *
 * NOTE: global-error replaces the root layout, so globals.css / Tailwind are
 * NOT available here — styles must be inline.
 */
export default function GlobalError({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  useEffect(() => {
    console.error("[GlobalError]", error);
  }, [error]);

  return (
    <html lang="en">
      <body style={{ margin: 0 }}>
        <div
          style={{
            minHeight: "100vh",
            display: "flex",
            flexDirection: "column",
            alignItems: "center",
            justifyContent: "center",
            gap: "20px",
            padding: "24px",
            textAlign: "center",
            background: "linear-gradient(180deg, #F5F9FC 0%, #E8F4FB 100%)",
            fontFamily:
              "system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif",
            color: "#00294A",
          }}
        >
          <div
            style={{
              width: 72,
              height: 72,
              borderRadius: "9999px",
              display: "grid",
              placeItems: "center",
              background: "rgba(0,151,220,0.12)",
              color: "#0097DC",
              fontSize: 34,
              fontWeight: 700,
            }}
            aria-hidden="true"
          >
            !
          </div>

          <h1 style={{ fontSize: 26, fontWeight: 700, margin: 0 }}>
            Something went wrong
          </h1>
          <p
            style={{
              fontSize: 16,
              lineHeight: 1.6,
              margin: 0,
              maxWidth: 460,
              color: "#4B4B4D",
            }}
          >
            We&apos;re having trouble loading the page right now. This is usually
            temporary — please try again in a moment.
          </p>

          <button
            type="button"
            onClick={() => reset()}
            style={{
              marginTop: 4,
              padding: "12px 28px",
              borderRadius: "9999px",
              border: "none",
              background: "linear-gradient(90deg, #0097DC 0%, #0077B6 100%)",
              color: "#ffffff",
              fontSize: 15,
              fontWeight: 600,
              cursor: "pointer",
            }}
          >
            Try again
          </button>

          {error.digest && (
            <p
              style={{
                margin: 0,
                fontSize: 12,
                letterSpacing: "0.02em",
                color: "#8A8A8C",
              }}
            >
              Reference: {error.digest}
            </p>
          )}
        </div>
      </body>
    </html>
  );
}
