import { Component } from 'react'

export default class ErrorBoundary extends Component {
  constructor(props) {
    super(props)
    this.state = { hasError: false, error: null }
  }

  static getDerivedStateFromError(error) {
    return { hasError: true, error }
  }

  componentDidCatch(error, info) {
    console.error('ErrorBoundary caught:', error, info)
  }

  render() {
    if (this.state.hasError) {
      return (
        <div style={{ padding: '80px 24px', textAlign: 'center' }}>
          <h1>Something went wrong</h1>
          <p style={{ color: '#666', marginBottom: 24 }}>
            {this.state.error?.message || 'An unexpected error occurred'}
          </p>
          <button
            onClick={() => { this.setState({ hasError: false, error: null }) }}
            style={{
              padding: '12px 24px', background: '#FA2D39', color: '#fff',
              border: 'none', borderRadius: 6, cursor: 'pointer', fontSize: 16
            }}
          >
            Try again
          </button>
        </div>
      )
    }
    return this.props.children
  }
}
