import Documents from './pages/Documents';
import Datastage from './pages/Datastage';
import Agenda from './pages/Agenda';
import Vucem from './pages/Vucem';
import Auditor from './pages/Auditor';
import { BrowserRouter, Routes, Route, useLocation } from 'react-router-dom';
import { UserProvider } from './context/UserContext';
import Navbar from './components/Navbar';
import Layout from './components/Layout';
import Login from './pages/Login';
import Admin from './pages/Admin';
import RequireAuth from './components/RequireAuth';
import LandingAnimated from './pages/LandingAnimated';
import Expedientes from './pages/Expedientes';
import Organization from './pages/Organization';
import Users from './pages/Users';
import UserForm from './pages/UserForm';
import Reports from './pages/Reports';
import Settings from './pages/Settings';
import Importers from './pages/Importers';
import PedimentoDetail from './pages/PedimentoDetail';
import Procesos from './pages/Procesos';
import TableroAlmacenamiento from './pages/TableroAlmacenamiento';
import Notificaciones from './pages/Notificaciones';
import ForgotPassword from './pages/ForgotPassword';
import PasswordResetConfirm from './pages/PasswordResetConfirm';

// Componente para manejar el layout condicional
function AppContent() {
  const location = useLocation();
  const isAuthPage = location.pathname === '/login' || location.pathname === '/' || location.pathname === '/forgot-password' || location.pathname.startsWith('/user/password-reset-confirm/');

  if (isAuthPage) {
    return (
      <>
        <Routes>
          <Route path="/" element={<LandingAnimated />} />
          <Route path="/login" element={<Login />} />
          <Route path="/forgot-password" element={<ForgotPassword />} />
          <Route path="/user/password-reset-confirm/:uid/:token/" element={<PasswordResetConfirm />} />
        </Routes>
      </>
    );
  }

  return (
    <Layout>
      <Routes>
        <Route path="/admin" element={
          <RequireAuth>
            <Admin />
          </RequireAuth>
        } />
        <Route path="/expedientes" element={
          <RequireAuth>
            <Expedientes />
          </RequireAuth>
        } />
        <Route path="/documents" element={
          <RequireAuth>
            <Documents />
          </RequireAuth>
        } />
        
        <Route path="/expedientes/pedimento/:id" element={
          <RequireAuth>
            <PedimentoDetail />
          </RequireAuth>
        } />
        <Route path="/organization" element={
          <RequireAuth>
            <Organization />
          </RequireAuth>
        } />
        <Route path="/users" element={
          <RequireAuth>
            <Users />
          </RequireAuth>
        } />
        <Route path="/users/new" element={
          <RequireAuth>
            <UserForm />
          </RequireAuth>
        } />
        <Route path="/users/:id/edit" element={
          <RequireAuth>
            <UserForm />
          </RequireAuth>
        } />
        <Route path="/reports" element={
          <RequireAuth>
            <Reports />
          </RequireAuth>
        } />
        <Route path="/settings" element={
          <RequireAuth>
            <Settings />
          </RequireAuth>
        } />
        <Route path="/notificaciones" element={<Notificaciones />} />
        {/* Ruta para importadores */}
        <Route path="/importers" element={
          <RequireAuth>
            <Importers />
          </RequireAuth>
        } />
        {/* Ruta para procesos */}
        <Route path="/procesos" element={
          <RequireAuth>
            <Procesos />
          </RequireAuth>
        } />
        {/* Ruta para Datastage */}
        <Route path="/datastage" element={
          <RequireAuth>
            <Datastage />
          </RequireAuth>
        } />
        {/* Ruta para agenda */}
        <Route path="/agenda" element={
          <RequireAuth>
            <Agenda />
          </RequireAuth>
        } />
        {/* Ruta para Uso de Almacenamiento */}
        <Route path="/tablero/cumplimiento" element={
          <RequireAuth>
            <TableroAlmacenamiento />
          </RequireAuth>
        } />
        {/* Ruta para Vucem */}
        <Route path="/vucem" element={
          <RequireAuth>
            <Vucem />
          </RequireAuth>
        } />
        {/* Ruta para Auditor */}
        <Route path="/auditor" element={
          <RequireAuth>
            <Auditor />
          </RequireAuth>
        } />
      </Routes>
    </Layout>
  );
}

function App() {
  return (
    <BrowserRouter>
      <UserProvider>
        <AppContent />
      </UserProvider>
    </BrowserRouter>
  );
}

export default App;