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 { useServerEnvironment } from 'state/useServerEnvironment';
import { useInitialData } from '@/state/useInitialData';

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

  if (isLoading || isServerSettingsLoading) return null;

  return (
    <BrowserRouter>
      {!isAuthenticated && (
        <Authentication.AuthenticationLayout>
          <Routes>
            <Route path="/" element={<Authentication.LandingPage />} />
            <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 && (
        <Dashboard.DashboardLayout>
          <Routes>
            <Route path="/" element={<Dashboard.HomeScreen />} />
            <Route path="/settings/profile" element={<Dashboard.ProfileSettings />} />
            <Route path="/settings/ai" element={<Dashboard.AISettings />} />
            <Route path="/settings/system" element={<Dashboard.SystemSettings />} />
            <Route path="/settings/integrations" element={<Dashboard.IntegrationsSettings />} />
            <Route path="/settings/apps" element={<Dashboard.AppsSettings />} />
            <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="/music" element={<Dashboard.MusicScreen />} />
            <Route path="/soulseek" element={<Dashboard.SoulseekScreen />} />
            <Route path="/headscale" element={<Dashboard.HeadscaleScreen />} />
            <Route path="/headscale/:section" element={<Dashboard.HeadscaleScreen />} />
            <Route path="/transmission" element={<Dashboard.TransmissionScreen />} />
            <Route path="/transmission/:section" element={<Dashboard.TransmissionScreen />} />
            <Route path="/invoices" element={<Dashboard.InvoicesScreen />} />
            <Route path="/invoices/:section" element={<Dashboard.InvoicesScreen />} />
            <Route path="/wallet" element={<Dashboard.WalletScreen />} />
            <Route path="/wallet/:section" element={<Dashboard.WalletScreen />} />
            <Route path="/system-monitor" element={<Dashboard.SystemMonitorScreen />} />
            <Route path="/activity" element={<Dashboard.ActivityScreen />} />

            <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="/jobs" element={<Dashboard.JobsPage />} />
            <Route path="/jobs/:id" element={<Dashboard.JobsPage />} />
            <Route path="/dashboards" element={<Dashboard.DashboardsScreen />} />
            <Route path="/dashboards/:id" element={<Dashboard.DashboardScreen />} />
            <Route path="/email" element={<Dashboard.EmailScreen />} />
            <Route path="/email/:emailId" element={<Dashboard.EmailScreen />} />
            <Route path="/browser" element={<Dashboard.BrowserScreen />} />
            <Route path="/terminal" element={<Dashboard.TerminalScreen />} />
            <Route path="/desktop" element={<Dashboard.DesktopScreen />} />
            <Route path="/auth/signout" element={<Authentication.SignoutScreen />} />
            <Route path="*" element={<Navigate to="/" replace />} />
          </Routes>
        </Dashboard.DashboardLayout>
      )}
    </BrowserRouter>
  );
}
