import { BrowserRouter, Routes, Route, Navigate } from 'react-router';
import * as Authentication from './Screens/Authentication';
import * as Dashboard from './Screens/Dashboard';
import { useAuth } from 'hooks/useAuth';
import { useServerSettings } from 'state/useServerSettings';
import { useInitialData } from '@/state/useInitialData';

export function App() {
  const { isLoading, isAuthenticated, user } = useAuth();
  const { onboardingComplete, plugins, isLoading: isServerSettingsLoading } = useServerSettings();
  useInitialData();

  if (isLoading || isServerSettingsLoading) return null;

  return (
    <BrowserRouter>
      {!isAuthenticated && (
        <Authentication.AuthenticationLayout>
          <Routes>
            <Route path="/" element={<Authentication.LandingPage />} />
            <Route path="/auth/verify" element={<Authentication.VerifyScreen />} />
            <Route path="/auth/forgot-password" element={<Authentication.ForgotPassword />} />
            <Route path="/auth/reset-password" element={<Authentication.ResetPassword />} />
            <Route path="*" element={<Navigate to="/" replace />} />
          </Routes>
        </Authentication.AuthenticationLayout>
      )}
      {/* {isAuthenticated && !onboardingComplete && ( */}
      {/*   <Routes> */}
      {/*     <Route path="/onboarding-admin" element={<OnboardingAdmin />} /> */}
      {/*     <Route path="/auth/signout" element={<SignoutScreen />} /> */}
      {/*     <Route path="*" element={<Navigate to="/onboarding-admin" replace />} /> */}
      {/*   </Routes> */}
      {/* )} */}
      {isAuthenticated && onboardingComplete && (
        <Dashboard.DashboardLayout>
          <Routes>
            <Route path="/" element={<Dashboard.HomeScreen />} />
            <Route path="/settings/profile" element={<Dashboard.ProfileSettings />} />
            <Route path="/settings/system" element={user?.role !== 'Member' ? <Dashboard.SystemSettings /> : <Navigate to="/" replace />} />
            <Route path="/settings/resources" element={user?.role !== 'Member' ? <Dashboard.ResourceSettings /> : <Navigate to="/" replace />} />
            <Route path="/settings/users" element={user?.role === 'Super Admin' ? <Dashboard.UserSettings /> : <Navigate to="/" replace />} />
            <Route path="/settings/integrations" element={<Dashboard.IntegrationsSettings />} />
            <Route path="/automation" element={<Dashboard.Automation />} />
            <Route path="/chat" element={<Dashboard.SessionListPage />} />
            <Route path="/chat/new" element={<Dashboard.SessionListPage isNew />} />
            <Route path="/chat/:sessionId" element={<Dashboard.SessionListPage />} />
            <Route path="/plans" element={<Dashboard.Plans />} />
            <Route path="/files" element={<Dashboard.FilesScreen />} />

            <Route path="/code-editor" element={<Dashboard.CodeEditor />} />
            <Route path="/skills" element={<Dashboard.Skills />} />
            <Route path="/tasks" element={<Dashboard.Tasks />} />
            <Route path="/processes" element={<Dashboard.Processes />} />
            <Route path="/task-logs" element={<Dashboard.TaskLogs />} />
            <Route path="/workspaces" element={<Dashboard.WorkspacesScreen />} />
            <Route path="/workspaces/:id" element={<Dashboard.WorkspaceScreen />} />
            <Route path="/projects" element={<Dashboard.ProjectListScreen />} />
            <Route path="/projects/new" element={<Dashboard.NewProjectRedirect />} />
            <Route path="/projects/:id" element={<Dashboard.ProjectScreen />} />
            <Route path="/email" element={<Dashboard.EmailScreen />} />
            <Route path="/terminal" element={<Dashboard.TerminalScreen />} />
            <Route path="/auth/signout" element={<Authentication.SignoutScreen />} />
            <Route path="*" element={<Navigate to="/" replace />} />
          </Routes>
        </Dashboard.DashboardLayout>
      )}
    </BrowserRouter>
  );
}
