import { Navigate, Outlet, Route, Routes } from 'react-router-dom';
import { useAuth } from './context/AuthProvider';
import { Layout } from './layout/Layout';
import TasksPage from '../pages/TasksPage';
import FormsPage from '../pages/FormsPage';
import TaskPage from '../pages/TaskPage/TaskPage';
import FormPage from '../pages/FormPage';
import TablePage from '../pages/TablePage';
import TableTempPage from '../pages/TableTempPage/TableTempPage';
import LoginPage from '../pages/LoginPage';
import UsersPage from '../pages/AdminPanel/UsersPage/UsersPage';
import SspsPage from '../pages/AdminPanel/SppsPage/SppsPage';
import DictVspPage from '../pages/DictVspPage/DictVspPage.jsx';
import DictsNavigatorPage from '../pages/DictsNavigatorPage/DictsNavigatorPage.jsx';
import AuditLogsPage from '../pages/AdminPanel/AuditLogsPage/AuditLogsPage.jsx';
import { CircularProgress } from '@mui/material';
import NewTablePage from '../pages/NewTablePage.jsx';
import TablesTest from '../pages/TablesTest.jsx';
import NewFormTablePage from '../pages/NewFormTablePage.jsx'

export const PrivateRoute = () => {
  const { isAuthenticated, loading } = useAuth();

  if (loading) {
    <CircularProgress />;
  }

  return isAuthenticated ? <Outlet /> : <Navigate to="/login" replace />;
};

export const AppRoutes = () => {
  return (
    <Routes>
      <Route path="/login" element={<LoginPage />} />

      <Route element={<Layout />}>
        <Route element={<PrivateRoute />}>
          <Route path="/" element={<TasksPage />} />
          <Route path="/tasks" element={<TasksPage />} />
          <Route path="/forms" element={<FormsPage />} />
          <Route path="/task/:taskId" element={<TaskPage />} />
          <Route path="/form/:formId" element={<FormPage />} />
          <Route path="/table/:tableId" element={<TablePage />} />
          <Route path="/table-temp/:tableId" element={<TableTempPage />} />
          <Route path="/admin_panel/users" element={<UsersPage />} />
          <Route path="/admin_panel/ssps" element={<SspsPage />} />
          <Route path="/admin_panel/audit-log" element={<AuditLogsPage />} />
          <Route path="/dicts/dict-vsp" element={<DictVspPage />} />
          <Route path="/dicts-navigator" element={<DictsNavigatorPage />} />
          <Route path="/new-table" element={<NewTablePage />} />
          <Route path="/tables-new" element={<TablesTest />} />
          <Route path="/tables/:form/:sheetName" element={<NewFormTablePage />} />
          <Route path="/table/form/:formId/form-type/:formType/:sheetName" element={<NewFormTablePage />} />
        </Route>
      </Route>
    </Routes>
  );
};
