"use client";

import * as Sentry from "@sentry/nextjs";
import { useEffect } from "react";

/**
 * Ultimate fallback boundary: the root layout (and its globals.css import) is
 * dead here, so we render a fully self-contained page with inline styles rather
 * than relying on Tailwind. Sentry still captures the error on mount.
 */
export default function GlobalError({
  error,
}: {
  error: Error & { digest?: string };
}) {
  useEffect(() => {
    Sentry.captureException(error);
  }, [error]);

  return (
    <html lang="fr">
      <body
        style={{
          margin: 0,
          minHeight: "100vh",
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          padding: "2rem",
          fontFamily:
            "system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif",
          backgroundColor: "#fafafa",
          color: "#0a0a0a",
        }}
      >
        <div style={{ maxWidth: "28rem", textAlign: "center" }}>
          <h1 style={{ fontSize: "1.25rem", fontWeight: 600, margin: 0 }}>
            Une erreur est survenue
          </h1>
          <p
            style={{
              marginTop: "0.5rem",
              fontSize: "0.875rem",
              color: "#525252",
            }}
          >
            Une erreur inattendue a interrompu l'application. Rechargez la page
            pour continuer.
          </p>
          <div
            style={{
              marginTop: "1.5rem",
              display: "flex",
              flexWrap: "wrap",
              gap: "0.5rem",
              justifyContent: "center",
            }}
          >
            <button
              type="button"
              onClick={() => window.location.reload()}
              style={{
                cursor: "pointer",
                borderRadius: "0.5rem",
                border: "1px solid transparent",
                backgroundColor: "#0a0a0a",
                color: "#fafafa",
                padding: "0.5rem 1rem",
                fontSize: "0.875rem",
                fontWeight: 500,
              }}
            >
              Recharger la page
            </button>
            <a
              href="/keywords/reservoir"
              style={{
                display: "inline-block",
                borderRadius: "0.5rem",
                border: "1px solid #e5e5e5",
                backgroundColor: "#ffffff",
                color: "#0a0a0a",
                padding: "0.5rem 1rem",
                fontSize: "0.875rem",
                fontWeight: 500,
                textDecoration: "none",
              }}
            >
              Retour au dashboard
            </a>
          </div>
          {error.digest ? (
            <p
              style={{
                marginTop: "1.5rem",
                fontSize: "0.75rem",
                color: "#737373",
              }}
            >
              Code : {error.digest}
            </p>
          ) : null}
        </div>
      </body>
    </html>
  );
}
