import React from 'react';
import ErrorPage from '../Pages/ErrorPage';

/**
 * Error Boundary Component
 * Fängt React-Fehler ab und zeigt die 500-Error-Page an
 */
class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false, error: null, errorInfo: null };
  }

  static getDerivedStateFromError(error) {
    // Update state so the next render will show the fallback UI
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    // Log error details for debugging
    console.error('ErrorBoundary caught an error:', error, errorInfo);
    this.setState({
      error,
      errorInfo
    });
  }

  render() {
    if (this.state.hasError) {
      // Render 500 Error Page
      return <ErrorPage errorCode="500" />;
    }

    return this.props.children;
  }
}

export default ErrorBoundary;
