import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import CounterLayout from './layouts/CounterLayout';
import FastEntryPage from './pages/FastEntryPage';
import CashRegisterPage from './pages/CashRegisterPage';
import AdminDashboard from './pages/AdminDashboard';
import AdvancedAnalytics from './pages/AdvancedAnalytics';
import LoginPage from './pages/LoginPage';
import { ToastProvider } from './context/ToastContext';
import HistoryPage from './pages/HistoryPage';
import TreasuryPage from './pages/TreasuryPage';

// Componente simple de protección
const PrivateRoute = ({ children }: { children: React.ReactNode }) => {
  const token = localStorage.getItem('token');
  return token ? <>{children}</> : <Navigate to="/login" />;
};

function App() {
  return (
    <ToastProvider>
      <BrowserRouter>
        <Routes>
          <Route path="/login" element={<LoginPage />} />

          <Route element={<PrivateRoute><CounterLayout /></PrivateRoute>}>
            <Route path="/" element={<Navigate to="/dashboard" replace />} />
            <Route path="/dashboard" element={<AdminDashboard />} />
            <Route path="/nuevo-aviso" element={<FastEntryPage />} />
            <Route path="/caja" element={<CashRegisterPage />} />
            <Route path="/analitica" element={<AdvancedAnalytics />} />
            <Route path="/historial" element={<HistoryPage />} />
            <Route path="/tesoreria" element={<TreasuryPage />} />
          </Route>
        </Routes>
      </BrowserRouter>
    </ToastProvider>
  );
}

export default App;