import React from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.tsx';
import './index.css';

class AppErrorBoundary extends React.Component<
  { children: React.ReactNode },
  { error: Error | null }
> {
  constructor(props: { children: React.ReactNode }) {
    super(props);
    this.state = { error: null };
  }

  static getDerivedStateFromError(error: Error) {
    return { error };
  }

  render() {
    if (this.state.error) {
      return (
        <div className="min-h-screen flex items-center justify-center p-6 bg-[var(--vl-bg)] text-[var(--vl-text)]">
          <div className="max-w-md w-full rounded-lg border border-[var(--vl-border)] bg-[var(--vl-surface)] p-5 space-y-2">
            <h2 className="text-base font-semibold text-[#F97316]">
              VLSub Web encountered a display error
            </h2>
            <p className="text-xs text-[var(--vl-text-muted)] break-words">
              {this.state.error.message}
            </p>
            <button
              type="button"
              onClick={() => window.location.reload()}
              className="px-3 py-1.5 text-xs font-medium rounded-md bg-[var(--vl-accent)] text-white cursor-pointer"
            >
              Reload
            </button>
          </div>
        </div>
      );
    }
    return this.props.children;
  }
}

createRoot(document.getElementById('root')!).render(
  <AppErrorBoundary>
    <App />
  </AppErrorBoundary>
);
