'use client'

import React from 'react'

interface Props {
  children: React.ReactNode
  name: string
}

interface State {
  hasError: boolean
  error?: Error
}

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

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

  componentDidCatch(error: Error, info: React.ErrorInfo) {
    console.error(`[SectionErrorBoundary: ${this.props.name}]`, error, info.componentStack)
  }

  render() {
    if (this.state.hasError) {
      return (
        <div className="py-10 text-center text-gray-400 bg-gray-50 border-y border-gray-100">
          <p className="text-sm">{this.props.name} — تعذّر التحميل</p>
        </div>
      )
    }
    return this.props.children
  }
}
