import * as Sentry from '@sentry/node'
import {Component, ErrorInfo, ReactNode} from 'react'
import {Button} from 'web/components/buttons/button'
import WhackABug from 'web/components/game/whack-a-bug'

interface Props {
  children?: ReactNode
  fallback?: ReactNode
}

interface State {
  hasError: boolean
  error?: Error
}

export class ErrorBoundary extends Component<Props, State> {
  constructor(props: Props) {
    super(props)
    this.state = {hasError: false}
  }

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

  componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    Sentry.captureException(error, {
      contexts: {
        react: {
          componentStack: errorInfo.componentStack,
        },
      },
    })
    console.error('ErrorBoundary caught an error:', error, errorInfo)
  }

  handleReset = () => {
    this.setState({hasError: false, error: undefined})
    window.location.reload()
  }

  render() {
    if (this.state.hasError) {
      if (this.props.fallback) {
        return this.props.fallback
      }

      return (
        <div className="flex min-h-screen flex-col items-center justify-center p-4">
          <div className="max-w-md text-center">
            <h1 className="mb-4 text-2xl font-bold text-ink-1000">Something went wrong</h1>
            <p className="mb-6 text-gray-600">
              We apologize for the inconvenience. An unexpected error occurred.
            </p>
            {this.state.error && (
              <details className="mb-6 text-left">
                <summary className="cursor-pointer text-sm text-gray-500 hover:text-gray-700">
                  Error details
                </summary>
                <pre className="mt-2 overflow-auto rounded p-2 text-xs">
                  {this.state.error.message}
                </pre>
              </details>
            )}
            <div className="flex gap-3 justify-center">
              <Button color="gray" onClick={this.handleReset}>
                Reload page
              </Button>
              <Button color="gray" onClick={() => window.location.replace('/')}>
                Go home
              </Button>
            </div>
            <div className="max-w-lg mx-auto mt-4">
              <WhackABug />
            </div>
          </div>
        </div>
      )
    }

    return this.props.children
  }
}
