import { BrowserRouter, Routes, Route, Navigate } from 'react-router';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { Toaster } from 'sonner';
import LoginPage from '@/pages/LoginPage';
import DashboardPage from '@/pages/DashboardPage';
import ReportsPage from '@/pages/ReportsPage';
import BranchesPage from '@/pages/BranchesPage';
import SettingsPage from '@/pages/SettingsPage';
import JournalPage from '@/pages/JournalPage';
import EmailSendersPage from '@/pages/EmailSendersPage';
import TransactionsPage from '@/pages/TransactionsPage';
import TransactionReviewPage from '@/pages/TransactionReviewPage';
import ReceiptPreviewPage from '@/pages/ReceiptPreviewPage';
import MainLayout from '@/components/layout/MainLayout';

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 30_000,
      retry: 1,
    },
  },
});

function App() {
  return (
    <QueryClientProvider client={queryClient}>
    <BrowserRouter>
      <Routes>
        {/* Public routes */}
        <Route path="/login" element={<LoginPage />} />
        <Route path="/transactions/:id/review" element={<TransactionReviewPage />} />

        {/* Protected routes with sidebar layout */}
        <Route element={<MainLayout />}>
          <Route path="/dashboard" element={<DashboardPage />} />
          <Route path="/transactions" element={<TransactionsPage />} />
          <Route path="/reports" element={<ReportsPage />} />
          <Route path="/branches" element={<BranchesPage />} />
          <Route path="/transactions/:id/receipt" element={<ReceiptPreviewPage />} />
          <Route path="/journal" element={<JournalPage />} />
          <Route path="/email-senders" element={<EmailSendersPage />} />
          <Route path="/settings" element={<SettingsPage />} />
        </Route>

        {/* Default redirect */}
        <Route path="/" element={<Navigate to="/login" replace />} />
      </Routes>
    </BrowserRouter>
    <Toaster position="bottom-right" richColors />
    </QueryClientProvider>
  );
}

export default App;
