import { Toaster } from "@/components/ui/toaster";
import { Toaster as Sonner } from "@/components/ui/sonner";
import { TooltipProvider } from "@/components/ui/tooltip";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { AuthProvider } from "@/contexts/AuthContext";
import { ProtectedRoute } from "@/components/auth/ProtectedRoute";
import { MainLayout } from "@/layouts/MainLayout";
import { LoginPage } from "@/pages/LoginPage";
import { Dashboard } from "@/pages/Dashboard";
import { AgentsPage } from "@/modules/rh/pages/AgentsPage";
import { AgentDetailsPage } from "@/modules/rh/pages/AgentDetailsPage";
import { SalaryStructurePage } from "@/modules/rh/pages/SalaryStructurePage";
import { PayrollRunsPage } from "@/modules/rh/pages/PayrollRunsPage";
import { ContractsPage } from "@/modules/rh/pages/ContractsPage";
import { BankAccountsPage } from "@/modules/rh/pages/BankAccountsPage";
import { PayrollPeriodsPage } from "@/modules/rh/pages/PayrollPeriodsPage";
import { PerformanceEvaluationsPage } from "@/modules/rh/pages/PerformanceEvaluationsPage";
import { UsersPage } from "@/modules/admin/pages/UsersPage";
import { RolesPage } from "@/modules/admin/pages/RolesPage";
import { AuditLogsPage } from "@/modules/admin/pages/AuditLogsPage";
import { MinistryList } from "@/pages/org/MinistryList";
import { OrgUnitList } from "@/pages/org/OrgUnitList";
import { PositionList } from "@/pages/org/PositionList";
import { TaxSettingsPage } from "./modules/rh/pages/TaxSettingsPage";
import { TaxBracketsPage } from "./modules/rh/pages/TaxBracketsPage";
import { AttendanceDashboard } from "./modules/rh/pages/AttendanceDashboard";
import { AttendanceSheetPage } from "./modules/rh/pages/AttendanceSheetPage";
import { BanksPage } from "./modules/common/pages/BanksPage";
import { FiscalYearsPage } from "./modules/budget/pages/FiscalYearsPage";
import { BudgetLinesPage } from "./modules/budget/pages/BudgetLinesPage";
import { BudgetExecutionPage } from "./modules/budget/pages/BudgetExecutionPage";
import { PaymentBatchesPage } from "./modules/treasury/pages/PaymentBatchesPage";
import { PaymentOrdersPage } from "./modules/treasury/pages/PaymentOrdersPage";
import { TreasuryPaymentsPage } from "./modules/treasury/pages/TreasuryPaymentsPage";
import { CashAccountsPage } from "./modules/treasury/pages/CashAccountsPage";
import { TreasuryEntriesPage } from "./modules/treasury/pages/TreasuryEntriesPage";
import { TreasuryPlanPage } from "./modules/treasury/pages/TreasuryPlanPage";
import { PaymentAuthorizationsPage } from "./modules/treasury/pages/PaymentAuthorizationsPage";
import { CashFlowPage } from "./modules/treasury/pages/CashFlowPage";
import { BankReconciliationPage } from "./modules/treasury/pages/BankReconciliationPage";
import NotFound from "./pages/NotFound";

const queryClient = new QueryClient();

const App = () => (
  <QueryClientProvider client={queryClient}>
    <AuthProvider>
      <TooltipProvider>
        <Toaster />
        <Sonner />
        <BrowserRouter>
          <Routes>
            {/* Public Routes */}
            <Route path="/login" element={<LoginPage />} />

            {/* Protected Routes */}
            <Route element={<ProtectedRoute><MainLayout /></ProtectedRoute>}>
              {/* Dashboard */}
              <Route path="/" element={<Dashboard />} />

              {/* Admin Module */}
              <Route path="/admin/users" element={<UsersPage />} />
              <Route path="/admin/roles" element={<RolesPage />} />
              <Route path="/admin/audit" element={<AuditLogsPage />} />

              {/* Org Module */}
              <Route path="/org/ministries" element={<MinistryList />} />
              <Route path="/org/units" element={<OrgUnitList />} />
              <Route path="/org/positions" element={<PositionList />} />

              {/* RH Module */}
              <Route path="/rh/agents" element={<AgentsPage />} />
              <Route path="/rh/agents/:id" element={<AgentDetailsPage />} />
              <Route path="/rh/contracts" element={<ContractsPage />} />
              <Route path="/rh/bank-accounts" element={<BankAccountsPage />} />
              <Route path="/rh/salary-grid" element={<SalaryStructurePage />} />
              <Route path="/rh/payroll-periods" element={<PayrollPeriodsPage />} />
              <Route path="/rh/payroll-runs" element={<PayrollRunsPage />} />
              <Route path="/rh/tax-settings" element={<TaxSettingsPage />} />
              <Route path="/rh/tax-brackets" element={<TaxBracketsPage />} />
              <Route path="/rh/evaluations" element={<PerformanceEvaluationsPage />} />
              <Route path="/rh/attendance" element={<AttendanceDashboard />} />
              <Route path="/rh/attendance/:id" element={<AttendanceSheetPage />} />

              {/* Budget Module */}
              <Route path="/budget/fiscal-years" element={<FiscalYearsPage />} />
              <Route path="/budget/lines" element={<BudgetLinesPage />} />
              <Route path="/budget/execution" element={<BudgetExecutionPage />} />

              {/* Treasury Module */}
              <Route path="/treasury/cash-accounts" element={<CashAccountsPage />} />
              <Route path="/treasury/entries" element={<TreasuryEntriesPage />} />
              <Route path="/treasury/plans" element={<TreasuryPlanPage />} />
              <Route path="/treasury/authorizations" element={<PaymentAuthorizationsPage />} />
              <Route path="/treasury/cash-flow" element={<CashFlowPage />} />
              <Route path="/treasury/reconciliation" element={<BankReconciliationPage />} />
              <Route path="/treasury/batches" element={<PaymentBatchesPage />} />
              <Route path="/treasury/orders" element={<PaymentOrdersPage />} />
              <Route path="/treasury/confirmations" element={<TreasuryPaymentsPage />} />

              {/* Common Module */}
              <Route path="/common/banks" element={<BanksPage />} />
            </Route>

            {/* Catch-all */}
            <Route path="*" element={<NotFound />} />
          </Routes>
        </BrowserRouter>
      </TooltipProvider>
    </AuthProvider>
  </QueryClientProvider>
);

export default App;
