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

interface Props {
  children: ReactNode;
}

interface State {
  hasError: boolean;
  error?: Error;
}

class ErrorBoundary extends Component<Props, State> {
  public state: State = {
    hasError: false,
  };

  public static getDerivedStateFromError(error: Error): State {
    return { hasError: true, error };
  }

  public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error('Uncaught error:', error, errorInfo);
  }

  public render() {
    if (this.state.hasError) {
      return (
        <div style={{
          minHeight: '100vh',
          backgroundColor: '#0a0b0d',
          color: '#ffffff',
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center',
          justifyContent: 'center',
          fontFamily: 'sans-serif',
          padding: '20px',
          textAlign: 'center',
        }}>
          <h1 style={{ color: '#34d399', fontSize: '24px', marginBottom: '12px' }}>SEPATU4D</h1>
          <p style={{ color: '#94a3b8', marginBottom: '20px' }}>Halaman sedang dimuat ulang, silakan klik tombol di bawah:</p>
          <a
            href="https://sepatu4d.org"
            style={{
              backgroundColor: '#10b981',
              color: '#000000',
              padding: '12px 24px',
              borderRadius: '8px',
              fontWeight: 'bold',
              textDecoration: 'none',
            }}
          >
            Menuju Situs Resmi SEPATU4D
          </a>
        </div>
      );
    }

    return this.props.children;
  }
}

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