import { BrowserRouter, Routes, Route, Navigate } from 'react-router';
import { useAuth } from 'hooks/useAuth';
import { useServerSettings } from '@/state/useServerSettings';
import { useInitialData } from '@/state/useInitialData';
import { LandingPage, AuthLayout } from './Screens/LandingPage';
import { Home } from './Screens/Dashboard/Home';
import { ProfileSettings } from './Screens/Dashboard/Settings/ProfileSettings';
import { ClaudeChat, OpenCodeChat, NewChat } from './Screens/Dashboard/Chat';
import { Screen as ClaudeSessions } from 'plugins/ChatHistory/client';
import { Plans } from './Screens/Dashboard/Plans';
import { Skills } from './Screens/Dashboard/Skills';
import { Tasks } from './Screens/Dashboard/Tasks';
import { Processes } from './Screens/Dashboard/Processes';
import { TaskLogs } from './Screens/Dashboard/TaskLogs';
import { SignoutScreen } from './Screens/Dashboard/SignoutScreen';
import { Screen as Files } from 'plugins/FileBrowser/client';
import { Screen as Terminal } from 'plugins/Terminal/client';
import { AISettings } from './Screens/Dashboard/Settings/AISettings';
import { ServerSettings } from './Screens/Dashboard/Settings/ServerSettings';
import { ResourceSettings } from './Screens/Dashboard/Settings/ResourceSettings';
import { OnboardingAdmin } from './Screens/Dashboard/OnboardingAdmin';

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

  if (isLoading || isServerSettingsLoading) return null;

  return (
    <BrowserRouter>
      {!isAuthenticated && (
        <Routes>
          <Route path="/" element={<LandingPage />} />
          <Route path="/auth/*" element={<AuthLayout />} />
          <Route path="*" element={<Navigate to="/" replace />} />
        </Routes>
      )}
      {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 && (
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/settings/profile" element={<ProfileSettings />} />
          <Route path="/settings/ai" element={<AISettings />} />
          <Route path="/settings/server" element={<ServerSettings />} />
          <Route path="/settings/resources" element={<ResourceSettings />} />
          <Route path="/chat" element={<ClaudeSessions />} />
          <Route path="/chat/new" element={<NewChat />} />
          <Route path="/chat/:sessionId" element={<ClaudeChat />} />
          <Route path="/chat/opencode/new" element={<OpenCodeChat />} />
          <Route path="/chat/opencode/:sessionId" element={<OpenCodeChat />} />
          {plugins?.FileBrowser !== false && <Route path="/files" element={<Files />} />}
          {plugins?.Terminal !== false && <Route path="/terminal" element={<Terminal />} />}
          <Route path="/plans" element={<Plans />} />
          <Route path="/skills" element={<Skills />} />
          <Route path="/tasks" element={<Tasks />} />
          <Route path="/processes" element={<Processes />} />
          <Route path="/task-logs" element={<TaskLogs />} />
          <Route path="/auth/signout" element={<SignoutScreen />} />
          <Route path="*" element={<Navigate to="/" replace />} />
        </Routes>
      )}
    </BrowserRouter>
  );
}
