import { Component, StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import AuthGate from "./AuthGate.jsx";
import "./App.css";

class AppErrorBoundary extends Component {
  constructor(props) {
    super(props);
    this.state = { error: null };
  }

  static getDerivedStateFromError(error) {
    return { error };
  }

  componentDidCatch(error, details) {
    console.error("Crypto Monitor failed to render", error, details);
  }

  render() {
    if (!this.state.error) return this.props.children;

    return (
      <main className="auth-page">
        <section className="auth-card auth-setup-card">
          <p className="auth-eyebrow">DISPLAY ERROR</p>
          <h1>The page could not be displayed</h1>
          <p>{this.state.error.message || "An unexpected frontend error occurred."}</p>
          <button type="button" onClick={() => window.location.reload()}>
            Reload page
          </button>
        </section>
      </main>
    );
  }
}

createRoot(document.getElementById("root")).render(
  <StrictMode>
    <AppErrorBoundary>
      <AuthGate>
        {(auth) => <App {...auth} />}
      </AuthGate>
    </AppErrorBoundary>
  </StrictMode>,
);
