import React from 'react'
import ReactDOM from 'react-dom/client'
import { AlertTriangle } from 'lucide-react'
import App from './App'
import './styles.css'

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

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

  componentDidCatch(error, errorInfo) {
    console.error("Nexstream UI Error:", error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      return (
        <div className="w-full h-screen bg-[#0a0b0e] flex flex-col items-center justify-center p-6 text-white text-center select-none">
          <div className="w-16 h-16 rounded-full bg-[#E50914]/20 border border-[#E50914] flex items-center justify-center mb-4 shadow-[0_0_25px_rgba(229,9,20,0.5)]">
            <AlertTriangle className="w-8 h-8 text-[#E50914]" />
          </div>
          <h2 className="text-xl font-bold mb-2">Something went wrong</h2>
          <p className="text-xs text-gray-400 max-w-md mb-5 font-mono">
            {this.state.error?.message || "An unexpected error occurred while rendering Nexstream."}
          </p>
          <button
            onClick={() => {
              localStorage.clear();
              window.location.reload();
            }}
            className="px-5 py-2 bg-[#E50914] hover:bg-[#c11119] text-white text-xs font-bold rounded-full transition-all shadow-lg cursor-pointer"
          >
            Reset Cache & Reload
          </button>
        </div>
      );
    }
    return this.props.children;
  }
}

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <ErrorBoundary>
      <App />
    </ErrorBoundary>
  </React.StrictMode>,
)
