import { Component, type ReactNode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.tsx';
import './index.css';

interface ErrorBoundaryState {
  hasError: boolean;
}

class AppErrorBoundary extends Component<{ children: ReactNode }, ErrorBoundaryState> {
  state: ErrorBoundaryState = { hasError: false };

  static getDerivedStateFromError(): ErrorBoundaryState {
    return { hasError: true };
  }

  render() {
    if (this.state.hasError) {
      return (
        <main className="min-h-[100dvh] flex items-center justify-center px-4 py-8">
          <div className="bellas-card max-w-md w-full p-6 text-center space-y-4">
            <h1 className="font-display text-2xl font-semibold text-[var(--ink-primary)]">
              Bellas Extensiones
            </h1>
            <p className="text-sm text-[var(--ink-secondary)]">
              Ocurrió un inconveniente al cargar la agenda. Recarga la página para continuar.
            </p>
            <button
              type="button"
              onClick={() => window.location.reload()}
              className="bellas-btn-primary w-full"
            >
              Recargar agenda
            </button>
          </div>
        </main>
      );
    }

    return this.props.children;
  }
}

createRoot(document.getElementById('root')!).render(
  <AppErrorBoundary>
    <App />
  </AppErrorBoundary>,
);
