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

console.log("[Boot] Initializing FarmOps scripts...");

// --- GLOBAL ERROR CATCHER ---
const reportCriticalError = (error: any) => {
    console.error("[Boot] Critical Error Caught:", error);
    const rootElement = document.getElementById('root');
    if (rootElement) {
        rootElement.innerHTML = `
            <div style="background: #0f172a; color: #f8fafc; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 40px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; text-align: center;">
                <div style="background: #1e293b; padding: 30px; border-radius: 16px; border: 1px solid #334155; max-width: 800px; width: 100%; box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5);">
                    <div style="background: #ef4444; width: 60px; height: 60px; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 20px;">
                        <svg style="width: 30px; height: 30px; color: white;" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path></svg>
                    </div>
                    <h1 style="font-size: 24px; font-weight: 800; margin-bottom: 10px; letter-spacing: -0.025em;">Application Boot Failed</h1>
                    <p style="color: #94a3b8; margin-bottom: 24px;">A script or storage error prevented FarmOps from starting on this machine.</p>
                    
                    <div style="background: #0f172a; padding: 20px; border-radius: 8px; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: 13px; border: 1px solid #334155; text-align: left; overflow: auto; max-height: 300px; color: #f1f5f9; line-height: 1.5;">
                        <div style="color: #f87171; font-weight: bold; margin-bottom: 8px;">${error?.name || 'Error'}: ${error?.message || 'Unknown initialization failure'}</div>
                        <div style="color: #64748b; font-size: 11px;">${error?.stack || 'No stack trace available'}</div>
                    </div>
                    
                    <div style="margin-top: 24px; display: flex; gap: 12px; justify-content: center;">
                        <button onclick="location.reload()" style="padding: 12px 24px; border-radius: 8px; background: #16a34a; color: white; border: none; font-weight: 700; cursor: pointer; font-size: 14px; transition: opacity 0.2s;">Retry Load</button>
                        <button onclick="localStorage.clear(); location.reload();" style="padding: 12px 24px; border-radius: 8px; background: #334155; color: white; border: none; font-weight: 700; cursor: pointer; font-size: 14px;">Reset Cache & Retry</button>
                    </div>
                    <p style="margin-top: 20px; font-size: 11px; color: #475569;">If this persists, your browser storage may be full or third-party CDNs are being blocked.</p>
                </div>
            </div>
        `;
    }
};

window.onerror = (message, source, lineno, colno, error) => {
    reportCriticalError(error || { message, source, stack: `at ${source}:${lineno}:${colno}` });
    return true;
};

window.onunhandledrejection = (event) => {
    reportCriticalError(event.reason);
};

const boot = () => {
    console.log("[Boot] Locating root element...");
    const rootElement = document.getElementById('root');
    if (!rootElement) {
      console.error("[Boot] Root element missing from DOM!");
      return;
    }

    try {
        console.log("[Boot] Triggering React mount...");
        const root = createRoot(rootElement);
        root.render(
          <React.StrictMode>
            <App />
          </React.StrictMode>
        );
        console.log("[Boot] React render loop active.");
    } catch (e) {
        reportCriticalError(e);
    }
};

boot();
