import { Routes, Route, Navigate } from 'react-router-dom';
import { useEffect, useState } from 'react';
import { useApp } from './context/AppContext';
import { ModalProvider } from './context/modals/useModal';
import { ModalRoot } from './context/modals/ModalRoot';
import { ToastProvider } from './context/toasts/useToast';
import { ToastRoot } from './context/toasts/ToastRoot';
import { LoaderProvider } from './context/loaders/useLoader';
import { LoaderRoot } from './context/loaders/LoaderRoot';
import { Navbar } from './components/Navbar';
import { Home } from './pages/Home';
import Login from './pages/Login';
import { Register } from './pages/Register';
import { ProtectedRoute } from './components/ProtectedRoute';
import { Boards } from './pages/Boards';
import { BoardCreate } from './pages/BoardCreate';
import { BoardEdit } from './pages/BoardEdit';
import { BoardDetail } from './pages/BoardDetail';
import { BoardEpics } from './pages/BoardEpics';
import CreateEpic from './pages/CreateEpic';
import { EpicDetail } from './pages/EpicDetail';
import { EditEpic } from './pages/EditEpic';
import { BoardWikis } from './pages/BoardWikis';
import CreateWiki from './pages/CreateWiki';
import { WikiDetail } from './pages/WikiDetail';
import { EditWiki } from './pages/EditWiki';
import { CardDetail } from './pages/CardDetail';
import { BoardDetailLayout } from './components/BoardDetailLayout';

const App = () => {
  const { token } = useApp();
  const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null);

  useEffect(() => {
    setIsAuthenticated(!!token);
  }, [token]);

  if (isAuthenticated === null) {
    return null;
  }
  return (
    <LoaderProvider>
      <ToastProvider>
        <ModalProvider>
          <div className="min-h-screen bg-gray-900 text-gray-100">
            <Navbar />
            <main>
              <Routes>
                <Route
                  path="/"
                  element={
                    isAuthenticated ? (
                      <Navigate to="/boards" replace />
                    ) : (
                      <Navigate to="/home" replace />
                    )
                  }
                />
                <Route path="/home" element={<Home />} />
                <Route path="/login" element={<Login />} />
                <Route path="/register" element={<Register />} />

                {/* Protected Routes */}
                <Route
                  path="/boards"
                  element={
                    <ProtectedRoute>
                      <Boards />
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/new"
                  element={
                    <ProtectedRoute>
                      <BoardCreate />
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id/edit"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <BoardEdit />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <BoardDetail />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id/epics"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <BoardEpics />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id/epics/new"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <CreateEpic />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id/epics/:epicId"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <EpicDetail />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id/epics/:epicId/edit"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <EditEpic />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id/wikis"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <BoardWikis />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id/wikis/new"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <CreateWiki />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id/wikis/:wikiId"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <WikiDetail />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id/wikis/:wikiId/edit"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <EditWiki />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
                <Route
                  path="/boards/:id/cards/:cardId"
                  element={
                    <ProtectedRoute>
                      <BoardDetailLayout>
                        <CardDetail />
                      </BoardDetailLayout>
                    </ProtectedRoute>
                  }
                />
              </Routes>
            </main>
            {/* Order matters for Z-Index: Loader (70) > Toast (60) > Modal (50) */}
            <LoaderRoot />
            <ToastRoot />
            <ModalRoot />
          </div>
        </ModalProvider>
      </ToastProvider>
    </LoaderProvider>
  );
};

export default App;
