import { Toaster } from "@/components/ui/toaster"
import { QueryClientProvider } from '@tanstack/react-query'
import { queryClientInstance } from '@/lib/query-client'
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import PageNotFound from './lib/PageNotFound';
import { AuthProvider, useAuth } from '@/lib/AuthContext';
import ScrollToTop from './components/ScrollToTop';
import { Navigate } from 'react-router-dom';
import ProtectedRoute from '@/components/ProtectedRoute';
import Login from '@/pages/Login';
import Register from '@/pages/Register';
import ForgotPassword from '@/pages/ForgotPassword';
import ResetPassword from '@/pages/ResetPassword';
import AppExperience from '@/components/studio/AppExperience';
import Dashboard from '@/pages/Dashboard';
import Patients from '@/pages/Patients';
import Cases from '@/pages/Cases';
import Scans from '@/pages/Scans';
import DesignStudio from '@/pages/DesignStudio';
import Manufacturing from '@/pages/Manufacturing';
import Quality from '@/pages/Quality';
import Reports from '@/pages/Reports';
import Materials from '@/pages/Materials';
import Libraries from '@/pages/Libraries';
import Settings from '@/pages/Settings';
import { runtimeConfig, notifyHost } from '@/lib/runtime-config';
import LicenseGate from '@/components/LicenseGate';
import { useEffect } from 'react';
// Add page imports here

const AuthenticatedApp = () => {
  useEffect(() => {
    notifyHost('READY', { mode: runtimeConfig.mode, edition: runtimeConfig.edition });
  }, []);
  const { isLoadingAuth, isLoadingPublicSettings, authError, navigateToLogin } = useAuth();

  // SHiRE and demo modes run without the standalone authentication gate.
  if (runtimeConfig.requireAuth && (isLoadingPublicSettings || isLoadingAuth)) {
    return (
      <div className="fixed inset-0 flex items-center justify-center">
        <div className="w-8 h-8 border-4 border-slate-200 border-t-slate-800 rounded-full animate-spin"></div>
      </div>
    );
  }

  // Render the main app
  return (
    <Routes>
      <Route path="/login" element={<Login />} />
      <Route path="/register" element={<Register />} />
      <Route path="/forgot-password" element={<ForgotPassword />} />
      <Route path="/reset-password" element={<ResetPassword />} />
      <Route element={runtimeConfig.requireAuth ? <ProtectedRoute unauthenticatedElement={<Navigate to="/login" replace />} /> : <AppExperience />}>
        {runtimeConfig.requireAuth ? (
        <Route element={<AppExperience />}>
          <Route path="/" element={<Dashboard />} />
          <Route path="/patients" element={<Patients />} />
          <Route path="/cases" element={<Cases />} />
          <Route path="/scans" element={<Scans />} />
          <Route path="/design" element={<DesignStudio />} />
          <Route path="/materials" element={<Materials />} />
          <Route path="/manufacturing" element={<Manufacturing />} />
          <Route path="/quality" element={<Quality />} />
          <Route path="/reports" element={<Reports />} />
          <Route path="/libraries" element={<Libraries />} />
          <Route path="/settings" element={<Settings />} />
        </Route>
        ) : (<>
          <Route path="/" element={<Dashboard />} />
          <Route path="/patients" element={<Patients />} />
          <Route path="/cases" element={<Cases />} />
          <Route path="/scans" element={<Scans />} />
          <Route path="/design" element={<DesignStudio />} />
          <Route path="/materials" element={<Materials />} />
          <Route path="/manufacturing" element={<Manufacturing />} />
          <Route path="/quality" element={<Quality />} />
          <Route path="/reports" element={<Reports />} />
          <Route path="/libraries" element={<Libraries />} />
          <Route path="/settings" element={<Settings />} />
        </>)}
      </Route>
      <Route path="*" element={<PageNotFound />} />
    </Routes>
  );
};


function App() {

  return (
    <LicenseGate>
    <AuthProvider>
      <QueryClientProvider client={queryClientInstance}>
        <Router>
          <ScrollToTop />
          <AuthenticatedApp />
        </Router>
        <Toaster />
      </QueryClientProvider>
    </AuthProvider>
    </LicenseGate>
  )
}

export default App