import React, { Component, ErrorInfo, ReactNode } from "react";

interface Props {
  children: ReactNode;
  fallback?: (error: Error, retry: () => void) => ReactNode;
}

interface State {
  hasError: boolean;
  error: Error | null;
}

/**
 * Error Boundary component to catch React errors
 * Displays a user-friendly error message instead of crashing
 */
export class ErrorBoundary extends Component<Props, State> {
  public constructor(props: Props) {
    super(props);
    this.state = { hasError: false, error: null };
  }

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

  public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error("ErrorBoundary caught an error:", error, errorInfo);
  }

  private handleRetry = () => {
    this.setState({ hasError: false, error: null });
  };

  public render() {
    if (this.state.hasError) {
      if (this.props.fallback && this.state.error) {
        return this.props.fallback(this.state.error, this.handleRetry);
      }

      return (
        <div className="flex min-h-screen items-center justify-center bg-gray-50 dark:bg-slate-950">
          <div className="w-full max-w-md rounded-lg border border-red-200 bg-white p-6 shadow-lg dark:border-red-900 dark:bg-slate-800">
            <h2 className="mb-4 text-lg font-semibold text-red-900 dark:text-red-200">
              Oops! Something went wrong
            </h2>
            <p className="mb-4 text-sm text-red-800 dark:text-red-300">
              {this.state.error?.message || "An unexpected error occurred"}
            </p>
            <details className="mb-4">
              <summary className="cursor-pointer text-xs font-medium text-red-700 dark:text-red-400">
                Error details
              </summary>
              <pre className="mt-2 overflow-auto rounded bg-red-50 p-2 text-xs dark:bg-red-950">
                {this.state.error?.stack}
              </pre>
            </details>
            <div className="flex gap-2">
              <button
                onClick={this.handleRetry}
                className="flex-1 rounded-md bg-red-600 px-4 py-2 text-sm font-medium text-white hover:bg-red-700 dark:hover:bg-red-800"
              >
                Try Again
              </button>
              <button
                onClick={() => window.location.href = "/"}
                className="flex-1 rounded-md border border-red-200 px-4 py-2 text-sm font-medium text-red-600 hover:bg-red-50 dark:border-red-800 dark:text-red-400 dark:hover:bg-red-950"
              >
                Go Home
              </button>
            </div>
          </div>
        </div>
      );
    }

    return this.props.children;
  }
}
