import { useEffect, lazy, Suspense } from 'react';
import { useLocation } from 'react-router-dom';
import { 
  BrowserRouter, 
  Routes, 
  Route, 
  Navigate 
} from 'react-router-dom';
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { LoadingProvider, useNavigationLoading, useLoading } from './shared/contexts/LoadingContext';
import { CookieConsentProvider } from './shared/contexts/CookieConsentContext';
import { CurrencyProvider } from './features/payments/contexts/CurrencyContext';
import { CompanySettingsProvider } from './shared/contexts/CompanySettingsContext';
import { AuthModalProvider } from './features/auth/contexts/AuthModalContext';
import { AntibotProvider } from './features/auth/contexts/AntibotContext';
import { RoomProvider } from './features/rooms/contexts/RoomContext';
import { logDebug } from './shared/utils/errorReporter';
import OfflineIndicator from './shared/components/OfflineIndicator';
import CookieConsentBanner from './shared/components/CookieConsentBanner';
import CookiePreferencesModal from './shared/components/CookiePreferencesModal';
import AnalyticsLoader from './shared/components/AnalyticsLoader';
import Loading from './shared/components/Loading';
import Preloader from './shared/components/Preloader';
import ScrollToTop from './shared/components/ScrollToTop';
import AuthModalManager from './features/auth/components/AuthModalManager';
import ResetPasswordRouteHandler from './features/auth/components/ResetPasswordRouteHandler';
import ErrorBoundaryRoute from './shared/components/ErrorBoundaryRoute';

import useAuthStore from './store/useAuthStore';
import useFavoritesStore from './store/useFavoritesStore';

import LayoutMain from './shared/components/LayoutMain';
import AdminLayout from './pages/AdminLayout';

import { 
  ProtectedRoute, 
  AdminRoute,
  StaffRoute,
  AccountantRoute,
  CustomerRoute,
  HousekeepingRoute
} from './features/auth/components';

const HomePage = lazy(() => import('./features/content/pages/HomePage'));
const DashboardPage = lazy(() => import('./pages/customer/DashboardPage'));
const RoomListPage = lazy(() => import('./pages/customer/RoomListPage'));
const RoomDetailPage = lazy(() => import('./pages/customer/RoomDetailPage'));
const SearchResultsPage = lazy(() => import('./pages/customer/SearchResultsPage'));
const FavoritesPage = lazy(() => import('./pages/customer/FavoritesPage'));
const MyBookingsPage = lazy(() => import('./pages/customer/MyBookingsPage'));
const BookingSuccessPage = lazy(() => import('./pages/customer/BookingSuccessPage') as Promise<{ default: React.ComponentType<any> }>);
const BookingDetailPage = lazy(() => import('./pages/customer/BookingDetailPage'));
const FullPaymentPage = lazy(() => import('./pages/customer/FullPaymentPage'));
const PaymentConfirmationPage = lazy(() => import('./pages/customer/PaymentConfirmationPage'));
const PaymentResultPage = lazy(() => import('./pages/customer/PaymentResultPage'));
const PayPalReturnPage = lazy(() => import('./pages/customer/PayPalReturnPage'));
const PayPalCancelPage = lazy(() => import('./pages/customer/PayPalCancelPage'));
const BoricaReturnPage = lazy(() => import('./pages/customer/BoricaReturnPage'));
const InvoicePage = lazy(() => import('./pages/customer/InvoicePage'));
const InvoiceEditPage = lazy(() => import('./pages/admin/InvoiceEditPage'));
const ProfilePage = lazy(() => import('./pages/customer/ProfilePage'));
const LoyaltyPage = lazy(() => import('./pages/customer/LoyaltyPage'));
const GroupBookingPage = lazy(() => import('./pages/customer/GroupBookingPage'));
const ComplaintPage = lazy(() => import('./pages/customer/ComplaintPage'));
const GuestRequestsPage = lazy(() => import('./pages/customer/GuestRequestsPage'));
const GDPRPage = lazy(() => import('./pages/customer/GDPRPage'));
const GDPRDeletionConfirmPage = lazy(() => import('./pages/customer/GDPRDeletionConfirmPage'));
const SessionManagementPage = lazy(() => import('./pages/customer/SessionManagementPage'));
const AboutPage = lazy(() => import('./features/content/pages/AboutPage'));
const ContactPage = lazy(() => import('./features/content/pages/ContactPage'));
const PrivacyPolicyPage = lazy(() => import('./features/content/pages/PrivacyPolicyPage'));
const TermsPage = lazy(() => import('./features/content/pages/TermsPage'));
const RefundsPolicyPage = lazy(() => import('./features/content/pages/RefundsPolicyPage'));
const CancellationPolicyPage = lazy(() => import('./features/content/pages/CancellationPolicyPage'));
const AccessibilityPage = lazy(() => import('./features/content/pages/AccessibilityPage'));
const FAQPage = lazy(() => import('./features/content/pages/FAQPage'));
const BlogPage = lazy(() => import('./features/content/pages/BlogPage'));
const BlogDetailPage = lazy(() => import('./features/content/pages/BlogDetailPage'));
const ServicesPage = lazy(() => import('./features/content/pages/ServicesPage'));
const ServiceDetailPage = lazy(() => import('./features/content/pages/ServiceDetailPage'));

const AdminDashboardPage = lazy(() => import('./pages/admin/DashboardPage'));
const InvoiceManagementPage = lazy(() => import('./pages/admin/InvoiceManagementPage'));
const PaymentManagementPage = lazy(() => import('./pages/admin/PaymentManagementPage'));
const UserManagementPage = lazy(() => import('./pages/admin/UserManagementPage'));
const GuestProfilePage = lazy(() => import('./pages/admin/GuestProfilePage'));
const GroupBookingManagementPage = lazy(() => import('./pages/admin/GroupBookingManagementPage'));
const AdminBookingManagementPage = lazy(() => import('./pages/admin/BookingManagementPage'));
const PageContentDashboardPage = lazy(() => import('./pages/admin/PageContentDashboard'));
const AnalyticsDashboardPage = lazy(() => import('./pages/admin/AnalyticsDashboardPage'));
const BusinessDashboardPage = lazy(() => import('./pages/admin/BusinessDashboardPage'));
const SettingsPage = lazy(() => import('./pages/admin/SettingsPage'));
const TaskManagementPage = lazy(() => import('./pages/admin/TaskManagementPage'));
const WorkflowManagementPage = lazy(() => import('./pages/admin/WorkflowManagementPage'));
const NotificationManagementPage = lazy(() => import('./pages/admin/NotificationManagementPage'));
const ReceptionDashboardPage = lazy(() => import('./pages/admin/ReceptionDashboardPage'));
const LoyaltyManagementPage = lazy(() => import('./pages/admin/LoyaltyManagementPage'));
const AdvancedRoomManagementPage = lazy(() => import('./pages/admin/AdvancedRoomManagementPage'));
const EditRoomPage = lazy(() => import('./pages/admin/EditRoomPage'));
const RatePlanManagementPage = lazy(() => import('./pages/admin/RatePlanManagementPage'));
const PackageManagementPage = lazy(() => import('./pages/admin/PackageManagementPage'));
const SecurityManagementPage = lazy(() => import('./pages/admin/SecurityManagementPage'));
const EmailCampaignManagementPage = lazy(() => import('./pages/admin/EmailCampaignManagementPage'));
const ReviewManagementPage = lazy(() => import('./pages/admin/ReviewManagementPage'));
const BlogManagementPage = lazy(() => import('./pages/admin/BlogManagementPage'));
const ComplaintManagementPage = lazy(() => import('./pages/admin/ComplaintManagementPage'));
const FinancialAuditTrailPage = lazy(() => import('./pages/admin/FinancialAuditTrailPage'));
const ComplianceReportingPage = lazy(() => import('./pages/admin/ComplianceReportingPage'));
const ApprovalManagementPage = lazy(() => import('./pages/admin/ApprovalManagementPage'));
const GDPRManagementPage = lazy(() => import('./pages/admin/GDPRManagementPage'));
const WebhookManagementPage = lazy(() => import('./pages/admin/WebhookManagementPage'));
const APIKeyManagementPage = lazy(() => import('./pages/admin/APIKeyManagementPage'));
const BackupManagementPage = lazy(() => import('./pages/admin/BackupManagementPage'));
const ServiceManagementPage = lazy(() => import('./pages/admin/ServiceManagementPage'));
const InventoryManagementPage = lazy(() => import('./pages/admin/InventoryManagementPage'));
const MaintenanceManagementPage = lazy(() => import('./pages/admin/MaintenanceManagementPage'));
const InspectionManagementPage = lazy(() => import('./pages/admin/InspectionManagementPage'));
const StaffShiftManagementPage = lazy(() => import('./pages/admin/StaffShiftManagementPage'));

const StaffDashboardPage = lazy(() => import('./pages/staff/DashboardPage'));
const StaffInventoryViewPage = lazy(() => import('./pages/staff/InventoryViewPage'));
const StaffShiftViewPage = lazy(() => import('./pages/staff/ShiftViewPage'));
const StaffBookingManagementPage = lazy(() => import('./pages/staff/BookingManagementPage'));
const StaffReceptionDashboardPage = lazy(() => import('./pages/staff/ReceptionDashboardPage'));
const StaffPaymentManagementPage = lazy(() => import('./pages/staff/PaymentManagementPage'));
const StaffAnalyticsDashboardPage = lazy(() => import('./pages/staff/AnalyticsDashboardPage'));
const StaffGuestProfilePage = lazy(() => import('./pages/staff/GuestProfilePage'));
const StaffAdvancedRoomManagementPage = lazy(() => import('./pages/staff/AdvancedRoomManagementPage'));
const ChatManagementPage = lazy(() => import('./pages/staff/ChatManagementPage'));
const GuestRequestManagementPage = lazy(() => import('./pages/staff/GuestRequestManagementPage'));
const GuestCommunicationPage = lazy(() => import('./pages/staff/GuestCommunicationPage'));
const IncidentComplaintManagementPage = lazy(() => import('./pages/staff/IncidentComplaintManagementPage'));
const UpsellManagementPage = lazy(() => import('./pages/staff/UpsellManagementPage'));
const StaffLayout = lazy(() => import('./pages/StaffLayout'));

const AccountantDashboardPage = lazy(() => import('./pages/accountant/DashboardPage'));
const AccountantPaymentManagementPage = lazy(() => import('./pages/accountant/PaymentManagementPage'));
const AccountantInvoiceManagementPage = lazy(() => import('./pages/accountant/InvoiceManagementPage'));
const AccountantAnalyticsDashboardPage = lazy(() => import('./pages/accountant/AnalyticsDashboardPage'));
const GLManagementPage = lazy(() => import('./pages/accountant/GLManagementPage'));
const AccountantApprovalManagementPage = lazy(() => import('./pages/accountant/ApprovalManagementPage'));
const FinancialReportsPage = lazy(() => import('./pages/accountant/FinancialReportsPage'));
const ReconciliationPage = lazy(() => import('./pages/accountant/ReconciliationPage'));
const AuditTrailPage = lazy(() => import('./pages/accountant/AuditTrailPage'));
const AccountantSecurityManagementPage = lazy(() => import('./pages/accountant/SecurityManagementPage'));
const AccountantLayout = lazy(() => import('./pages/AccountantLayout'));

const HousekeepingDashboardPage = lazy(() => import('./pages/housekeeping/DashboardPage'));
const HousekeepingTasksPage = lazy(() => import('./pages/housekeeping/TasksPage'));
const HousekeepingShiftViewPage = lazy(() => import('./pages/housekeeping/ShiftViewPage'));
const HousekeepingLayout = lazy(() => import('./pages/HousekeepingLayout'));

const AdminProfilePage = lazy(() => import('./pages/admin/ProfilePage'));
const StaffProfilePage = lazy(() => import('./pages/staff/ProfilePage'));
const AccountantProfilePage = lazy(() => import('./pages/accountant/ProfilePage'));

// Separate login pages for each role
const StaffLoginPage = lazy(() => import('./features/auth/pages/StaffLoginPage'));
const AdminLoginPage = lazy(() => import('./features/auth/pages/AdminLoginPage'));
const HousekeepingLoginPage = lazy(() => import('./features/auth/pages/HousekeepingLoginPage'));
const AccountantLoginPage = lazy(() => import('./features/auth/pages/AccountantLoginPage'));

const NotFoundPage = lazy(() => import('./shared/pages/NotFoundPage'));

// Component to track navigation changes - must be inside Router
const NavigationTracker: React.FC = () => {
  const { pathname } = useLocation();
  const { setNavigationLoading } = useLoading();
  
  useEffect(() => {
    // Show navigation loading when route changes
    setNavigationLoading(true);
    
    // Hide navigation loading after a short delay
    const timer = setTimeout(() => {
      setNavigationLoading(false);
    }, 300);
    
    return () => clearTimeout(timer);
  }, [pathname, setNavigationLoading]);
  
  return null;
};

// Wrapper component to use navigation loading hook inside CompanySettingsProvider
const PreloaderWrapper: React.FC = () => {
  const { isLoading } = useNavigationLoading();
  return <Preloader isLoading={isLoading} />;
};

function App() {
  
  const { 
    isAuthenticated, 
    userInfo, 
    logout, 
    initializeAuth 
  } = useAuthStore();
  
  const { 
    fetchFavorites, 
    syncGuestFavorites,
    loadGuestFavorites,
  } = useFavoritesStore();

  
  useEffect(() => {
    // Initialize auth asynchronously to validate cookies
    initializeAuth().catch((error) => {
      // SECURITY: Silently handle auth initialization errors
      // User will be prompted to login if needed
      // Only log in development to prevent information disclosure
      logDebug('Auth initialization failed', { error: error instanceof Error ? error.message : String(error) });
    });
  }, [initializeAuth]);

  
  useEffect(() => {
    if (isAuthenticated) {
      
      syncGuestFavorites().then(() => {
        fetchFavorites();
      });
    } else {
      
      loadGuestFavorites();
    }
  }, [
    isAuthenticated, 
    fetchFavorites, 
    syncGuestFavorites,
    loadGuestFavorites,
  ]);

  
  const handleLogout = async () => {
    await logout();
  };

  return (
    <LoadingProvider>
      <CookieConsentProvider>
        <CurrencyProvider>
          <CompanySettingsProvider>
            <AntibotProvider>
              <AuthModalProvider>
                <RoomProvider>
      <BrowserRouter
        future={{
          v7_startTransition: true,
          v7_relativeSplatPath: true,
        }}
      >
          <PreloaderWrapper />
          <ScrollToTop />
          <NavigationTracker />
          <Suspense fallback={<Loading fullScreen text="Loading page..." />}>
          <Routes>
        {}
        <Route 
          path="/" 
          element={
            <LayoutMain 
              isAuthenticated={isAuthenticated}
              userInfo={userInfo}
              onLogout={handleLogout}
            />
          }
        >
          <Route index element={<HomePage />} />
          <Route 
            path="rooms" 
            element={<RoomListPage />} 
          />
          <Route 
            path="rooms/search" 
            element={<SearchResultsPage />} 
          />
          <Route 
            path="rooms/:room_number" 
            element={<RoomDetailPage />} 
          />
          <Route 
            path="favorites" 
            element={
              <CustomerRoute>
                <FavoritesPage />
              </CustomerRoute>
            } 
          />
          <Route 
            path="payment-result" 
            element={
              <ErrorBoundaryRoute>
                <PaymentResultPage />
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="payment/paypal/return" 
            element={
              <ErrorBoundaryRoute>
                <PayPalReturnPage />
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="payment/paypal/cancel" 
            element={
              <ErrorBoundaryRoute>
                <PayPalCancelPage />
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="payment/borica/return" 
            element={
              <ErrorBoundaryRoute>
                <BoricaReturnPage />
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="invoices/:id" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <InvoicePage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="about" 
            element={<AboutPage />} 
          />
          <Route 
            path="contact" 
            element={<ContactPage />} 
          />
          <Route 
            path="privacy" 
            element={<PrivacyPolicyPage />} 
          />
          <Route 
            path="terms" 
            element={<TermsPage />} 
          />
          <Route 
            path="refunds" 
            element={<RefundsPolicyPage />} 
          />
          <Route 
            path="cancellation" 
            element={<CancellationPolicyPage />} 
          />
          <Route 
            path="accessibility" 
            element={<AccessibilityPage />} 
          />
          <Route 
            path="faq" 
            element={<FAQPage />} 
          />
          <Route 
            path="blog" 
            element={<BlogPage />} 
          />
          <Route 
            path="blog/:slug" 
            element={<BlogDetailPage />} 
          />
          <Route 
            path="services" 
            element={<ServicesPage />} 
          />
          <Route 
            path="services/:slug" 
            element={<ServiceDetailPage />} 
          />
          
          {}
          <Route 
            path="dashboard" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <DashboardPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="booking-success/:id" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <BookingSuccessPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="bookings" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <MyBookingsPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="bookings/:id" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <BookingDetailPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="payment/:bookingId" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <FullPaymentPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="payment-confirmation/:id" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <PaymentConfirmationPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="profile" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <ProfilePage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="loyalty" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <LoyaltyPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="group-bookings" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <GroupBookingPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="complaints" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <ComplaintPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="requests" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <GuestRequestsPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="gdpr" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <GDPRPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="gdpr/delete/confirm" 
            element={
              <ErrorBoundaryRoute>
                <GDPRDeletionConfirmPage />
              </ErrorBoundaryRoute>
            } 
          />
          <Route 
            path="sessions" 
            element={
              <ErrorBoundaryRoute>
                <CustomerRoute>
                  <SessionManagementPage />
                </CustomerRoute>
              </ErrorBoundaryRoute>
            } 
          />
        </Route>

        {/* Separate Login Pages for Each Role */}
        <Route 
          path="/staff/login" 
          element={
            <ErrorBoundaryRoute>
              <StaffLoginPage />
            </ErrorBoundaryRoute>
          } 
        />
        <Route 
          path="/admin/login" 
          element={
            <ErrorBoundaryRoute>
              <AdminLoginPage />
            </ErrorBoundaryRoute>
          } 
        />
        <Route 
          path="/housekeeping/login" 
          element={
            <ErrorBoundaryRoute>
              <HousekeepingLoginPage />
            </ErrorBoundaryRoute>
          } 
        />
        <Route 
          path="/accountant/login" 
          element={
            <ErrorBoundaryRoute>
              <AccountantLoginPage />
            </ErrorBoundaryRoute>
          } 
        />

        {}
        <Route 
          path="/reset-password/:token" 
          element={<ResetPasswordRouteHandler />} 
        />

        {}
        <Route 
          path="/admin" 
          element={
            <ErrorBoundaryRoute>
              <AdminRoute>
                <AdminLayout />
              </AdminRoute>
            </ErrorBoundaryRoute>
          }
        >
          <Route 
            index 
            element={<Navigate to="dashboard" replace />} 
          />
          <Route path="dashboard" element={<AdminDashboardPage />} />
          <Route 
            path="users" 
            element={<UserManagementPage />} 
          />
          <Route
            path="business" 
            element={<BusinessDashboardPage />} 
          />
          <Route 
            path="reception" 
            element={<ReceptionDashboardPage />} 
          />
          <Route 
            path="advanced-rooms" 
            element={<AdvancedRoomManagementPage />} 
          />
          <Route 
            path="rooms/:id/edit" 
            element={<EditRoomPage />} 
          />
          <Route 
            path="page-content" 
            element={<PageContentDashboardPage />} 
          />
          <Route 
            path="analytics" 
            element={<AnalyticsDashboardPage />} 
          />
          <Route 
            path="tasks" 
            element={<TaskManagementPage />} 
          />
          <Route 
            path="workflows" 
            element={<WorkflowManagementPage />} 
          />
          <Route 
            path="notifications" 
            element={<NotificationManagementPage />} 
          />
          <Route
            path="settings" 
            element={<SettingsPage />} 
          />
          <Route
            path="invoices" 
            element={<InvoiceManagementPage />} 
          />
          <Route
            path="invoices/:id/edit" 
            element={<InvoiceEditPage />} 
          />
          <Route
            path="invoices/:id" 
            element={<InvoicePage />} 
          />
          <Route
            path="payments" 
            element={<PaymentManagementPage />} 
          />
          <Route
            path="loyalty" 
            element={<LoyaltyManagementPage />} 
          />
          <Route
            path="guest-profiles" 
            element={<GuestProfilePage />} 
          />
          <Route
            path="group-bookings" 
            element={<GroupBookingManagementPage />} 
          />
          <Route
            path="bookings" 
            element={<AdminBookingManagementPage />} 
          />
          <Route
            path="rate-plans" 
            element={<RatePlanManagementPage />} 
          />
          <Route
            path="packages" 
            element={<PackageManagementPage />} 
          />
          <Route
            path="security" 
            element={<SecurityManagementPage />} 
          />
          <Route
            path="email-campaigns" 
            element={<EmailCampaignManagementPage />} 
          />
          <Route
            path="reviews" 
            element={<ReviewManagementPage />} 
          />
          <Route
            path="blog" 
            element={<BlogManagementPage />} 
          />
          <Route
            path="complaints" 
            element={<ComplaintManagementPage />} 
          />
          <Route
            path="financial-audit" 
            element={<FinancialAuditTrailPage />} 
          />
          <Route
            path="compliance" 
            element={<ComplianceReportingPage />} 
          />
          <Route
            path="approvals" 
            element={<ApprovalManagementPage />} 
          />
          <Route
            path="gdpr" 
            element={<GDPRManagementPage />} 
          />
          <Route
            path="webhooks" 
            element={<WebhookManagementPage />} 
          />
          <Route
            path="api-keys" 
            element={<APIKeyManagementPage />} 
          />
          <Route
            path="backups" 
            element={<BackupManagementPage />} 
          />
          <Route
            path="services" 
            element={<ServiceManagementPage />} 
          />
          <Route
            path="inventory" 
            element={<InventoryManagementPage />} 
          />
          <Route
            path="maintenance" 
            element={<MaintenanceManagementPage />} 
          />
          <Route
            path="inspections" 
            element={<InspectionManagementPage />} 
          />
          <Route
            path="shifts" 
            element={<StaffShiftManagementPage />} 
          />
          <Route
            path="profile" 
            element={<AdminProfilePage />} 
          />
          <Route
            path="sessions" 
            element={<SessionManagementPage />} 
          />
        </Route>

        {}
        <Route 
          path="/staff" 
          element={
            <ErrorBoundaryRoute>
              <StaffRoute>
                <StaffLayout />
              </StaffRoute>
            </ErrorBoundaryRoute>
          }
        >
          <Route 
            index 
            element={<Navigate to="dashboard" replace />} 
          />
          <Route path="dashboard" element={<StaffDashboardPage />} />
          <Route 
            path="bookings" 
            element={<StaffBookingManagementPage />} 
          />
          <Route 
            path="reception" 
            element={<StaffReceptionDashboardPage />} 
          />
          <Route
            path="payments" 
            element={<StaffPaymentManagementPage />} 
          />
          <Route 
            path="reports" 
            element={<StaffAnalyticsDashboardPage />} 
          />
          <Route 
            path="chats" 
            element={<ChatManagementPage />} 
          />
          <Route
            path="guest-profiles" 
            element={<StaffGuestProfilePage />} 
          />
          <Route
            path="advanced-rooms" 
            element={<StaffAdvancedRoomManagementPage />} 
          />
          <Route
            path="guest-requests" 
            element={<GuestRequestManagementPage />} 
          />
          <Route
            path="guest-communication" 
            element={<GuestCommunicationPage />} 
          />
          <Route
            path="incidents-complaints" 
            element={<IncidentComplaintManagementPage />} 
          />
          <Route
            path="upsells" 
            element={<UpsellManagementPage />} 
          />
          <Route
            path="invoices/:id" 
            element={<InvoicePage />} 
          />
          <Route
            path="profile" 
            element={<StaffProfilePage />} 
          />
          <Route
            path="sessions" 
            element={<SessionManagementPage />} 
          />
          <Route
            path="inventory" 
            element={<StaffInventoryViewPage />} 
          />
          <Route
            path="shifts" 
            element={<StaffShiftViewPage />} 
          />
        </Route>

        {/* Accountant Routes */}
        <Route 
          path="/accountant" 
          element={
            <ErrorBoundaryRoute>
              <AccountantRoute>
                <AccountantLayout />
              </AccountantRoute>
            </ErrorBoundaryRoute>
          }
        >
          <Route 
            index 
            element={<Navigate to="dashboard" replace />} 
          />
          <Route path="dashboard" element={<AccountantDashboardPage />} />
          <Route 
            path="payments" 
            element={<AccountantPaymentManagementPage />} 
          />
          <Route 
            path="invoices" 
            element={<AccountantInvoiceManagementPage />} 
          />
          <Route 
            path="gl" 
            element={<GLManagementPage />} 
          />
          <Route 
            path="approvals" 
            element={<AccountantApprovalManagementPage />} 
          />
          <Route 
            path="reports" 
            element={<FinancialReportsPage />} 
          />
          <Route 
            path="reconciliation" 
            element={<ReconciliationPage />} 
          />
          <Route 
            path="audit-trail" 
            element={<AuditTrailPage />} 
          />
          <Route 
            path="security" 
            element={<AccountantSecurityManagementPage />} 
          />
          <Route 
            path="profile" 
            element={<AccountantProfilePage />} 
          />
          <Route 
            path="sessions" 
            element={<SessionManagementPage />} 
          />
          <Route 
            path="invoices/:id/edit" 
            element={<InvoiceEditPage />} 
          />
          <Route 
            path="invoices/:id" 
            element={<InvoicePage />} 
          />
        </Route>

        {/* Housekeeping Routes */}
        <Route 
          path="/housekeeping" 
          element={
            <ErrorBoundaryRoute>
              <HousekeepingRoute>
                <HousekeepingLayout />
              </HousekeepingRoute>
            </ErrorBoundaryRoute>
          }
        >
          <Route 
            index 
            element={<Navigate to="dashboard" replace />} 
          />
          <Route path="dashboard" element={<HousekeepingDashboardPage />} />
          <Route path="tasks" element={<HousekeepingTasksPage />} />
          <Route path="shifts" element={<HousekeepingShiftViewPage />} />
          <Route path="sessions" element={<SessionManagementPage />} />
        </Route>

        {}
        <Route 
          path="*" 
          element={<NotFoundPage />} 
        />
      </Routes>
      
          <ToastContainer 
            position="top-right"
            autoClose={3000}
            hideProgressBar={false}
            newestOnTop
            closeOnClick
            rtl={false}
            pauseOnFocusLoss
            draggable
            pauseOnHover
            theme="light"
            toastClassName="rounded-lg shadow-lg"
            bodyClassName="text-sm font-medium"
          />
          <OfflineIndicator />
          <CookieConsentBanner />
          <CookiePreferencesModal />
          <AnalyticsLoader />
          <AuthModalManager />
        </Suspense>
      </BrowserRouter>
                </RoomProvider>
              </AuthModalProvider>
            </AntibotProvider>
          </CompanySettingsProvider>
        </CurrencyProvider>
      </CookieConsentProvider>
    </LoadingProvider>
  );
}

export default App;
