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

interface Props {
  children: ReactNode;
}

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

export class ErrorBoundary extends Component<Props, State> {
  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('Uncaught application error:', error, errorInfo);
  }

  public render() {
    if (this.state.hasError) {
      return (
        <div className="min-h-screen bg-[#1e2a38] text-white flex flex-col items-center justify-center p-6 text-center">
          <div className="max-w-md w-full bg-[#2b3b4e] p-8 rounded-2xl border border-[#517e8a]/40 shadow-2xl space-y-4">
            <h2 className="text-xl font-bold text-[#e17f52]">Entourage.hk</h2>
            <p className="text-sm text-slate-300">
              An unexpected error occurred while rendering the page.
            </p>
            <button
              onClick={() => window.location.reload()}
              className="px-6 py-2.5 rounded-xl bg-[#e17f52] hover:bg-[#ea8e64] text-white font-semibold text-xs uppercase tracking-wider transition-all cursor-pointer shadow-lg"
            >
              Reload Page
            </button>
          </div>
        </div>
      );
    }

    return this.props.children;
  }
}
