'use client';

import * as Sentry from '@sentry/nextjs';
import { Component, type ErrorInfo, type ReactNode } from 'react';

interface TodayWidgetErrorBoundaryProps {
  children: ReactNode;
  fallback: ReactNode;
}

interface TodayWidgetErrorBoundaryState {
  hasError: boolean;
}

export class TodayWidgetErrorBoundary extends Component<
  TodayWidgetErrorBoundaryProps,
  TodayWidgetErrorBoundaryState
> {
  state: TodayWidgetErrorBoundaryState = { hasError: false };

  static getDerivedStateFromError(): TodayWidgetErrorBoundaryState {
    return { hasError: true };
  }

  componentDidCatch(error: Error, info: ErrorInfo) {
    console.error('Today widget render error:', error, info);
    Sentry.captureException(error, { extra: { componentStack: info.componentStack } });
  }

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