import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import { GoogleOAuthProvider } from "@react-oauth/google";
import ThemeProvider from "@/theme/ThemeProvider";
import { ParticleThemeProvider } from "@/contexts/ParticleThemeContext";
// import { checkIntegrity } from "./utils/security"; // 🔮 TEMPORARILY DISABLED FOR ZODU DEVELOPMENT
import "/src/styles/tailwind.css";

// Import tarot data initialization function
import { initializeTarotData } from "./utils/tarotHelpers.js";
import { installChunkErrorRecovery } from "./utils/chunkReload.js";

// Before anything lazy-loads: a chunk from a replaced build reloads the page
// into the current release instead of stranding the seeker on an error.
installChunkErrorRecovery();

// === Config flags ===
const bypass = localStorage.getItem("bypassTamper") === "true";
const debug = import.meta.env.VITE_DEBUG === "true";
const isProd = import.meta.env.MODE === "production";
const clientId = import.meta.env.VITE_GOOGLE_CLIENT_ID;

// === ZODU.ai Development Mode ===
console.log("🔮 ZODU.ai - Zodiacal Oracle of Divine Understanding");
console.log("✨ Mystical platform initializing...");
console.log("🌟 Security temporarily relaxed for cosmic development");

// Check if Google Client ID is configured
if (!clientId) {
  console.warn("⚠️ Google Client ID not configured - Google sign-in will not work");
  console.warn("Please set VITE_GOOGLE_CLIENT_ID in your .env file");
} else {
  console.log("✅ Google OAuth configured with Client ID:", clientId.substring(0, 10) + "...");
}

// === Optional security: disable console in production (RELAXED FOR DEVELOPMENT) ===
if (isProd && !debug && false) { // Added "&& false" to disable console blocking during development
  console.log = () => { };
  console.warn = () => { };
  console.error = () => { };
}

// === Optional: block React DevTools injection (DISABLED FOR DEVELOPMENT) ===
if (isProd && typeof window !== "undefined" && window.__REACT_DEVTOOLS_GLOBAL_HOOK__ && false) { // Added "&& false"
  for (let key in window.__REACT_DEVTOOLS_GLOBAL_HOOK__) {
    window.__REACT_DEVTOOLS_GLOBAL_HOOK__[key] =
      typeof window.__REACT_DEVTOOLS_GLOBAL_HOOK__[key] === "function" ? () => { } : null;
  }
}

// === DISABLED: Run integrity check (THIS WAS CAUSING YOUR POPUP!) ===
// This checkIntegrity() call was triggering the security popup that blocked you
// We'll re-enable this later with ZODU-themed security messages
/*
if (!bypass && !debug) {
  checkIntegrity(); // <-- THIS LINE WAS THE CULPRIT!
}
*/

console.log("🌙 Cosmic security checks bypassed - Welcome to the mystical realm!");

// === Initialize tarot data and then render ===
const root = ReactDOM.createRoot(document.getElementById("root"));

// Initialize tarot data before rendering
initializeTarotData()
  .then(() => {
    console.log("🎴 Tarot data initialized successfully");

    // Render the application after tarot data is loaded
    root.render(
      <React.StrictMode>
        <ThemeProvider>
          <ParticleThemeProvider>
            <GoogleOAuthProvider clientId={clientId}>
              <App />
            </GoogleOAuthProvider>
          </ParticleThemeProvider>
        </ThemeProvider>
      </React.StrictMode>
    );
  })
  .catch(error => {
    // Log the error but still render the app with fallback data
    console.error("⚠️ Error initializing tarot data:", error);
    console.log("🔮 Using fallback tarot data instead");

    root.render(
      <React.StrictMode>
        <ThemeProvider>
          <ParticleThemeProvider>
            <GoogleOAuthProvider clientId={clientId}>
              <App />
            </GoogleOAuthProvider>
          </ParticleThemeProvider>
        </ThemeProvider>
      </React.StrictMode>
    );
  });

/* 
🔮 ZODU.ai DEVELOPMENT NOTES:
- Security popup disabled during platform rebranding
- Integrity checks temporarily commented out  
- Console access enabled for development
- React DevTools allowed during development
- Background security logging still active via server
- Tarot data initialization added before rendering

TODO After Rebranding Complete:
1. Create ZODU-themed security messages
2. Re-enable integrity checks with mystical flair
3. Update security popup to match cosmic branding
4. Replace all "LoquiSec" references with "ZODUSec"
5. Test production security with mystical messaging
*/






