import { BrowserRouter, Routes, Route } from 'react-router-dom'
import { AuthProvider } from './context/AuthContext'
import { CustomerThemeProvider } from './context/CustomerThemeContext'
import { TenantBrandingProvider } from './context/TenantBrandingContext'
import { LocaleProvider, ToastProvider } from '@meshkee/dashboard-ui'
import { CustomerDomainGuard } from './components/CustomerDomainGuard'
import { DashboardDocumentTitle } from './components/DashboardDocumentTitle'
import { ProtectedRoute } from './components/ProtectedRoute'
import { GuestRoute } from './components/GuestRoute'
import { PageLayout } from './components/PageLayout'
import { HomePage } from './pages/HomePage'
import { ProfilePage } from './pages/ProfilePage'
import { OrdersPage } from './pages/OrdersPage'
import { AddressesPage } from './pages/AddressesPage'
import { FavoritesPage } from './pages/FavoritesPage'
import { LoginPage } from './pages/LoginPage'
import { CheckoutLayout } from './components/checkout/CheckoutLayout'
import { CheckoutFlow } from './pages/checkout/CheckoutFlow'
import { CheckoutLoginStep } from './pages/checkout/CheckoutLoginStep'
import { CheckoutCartStep } from './pages/checkout/CheckoutCartStep'
import { CheckoutDeliveryStep } from './pages/checkout/CheckoutDeliveryStep'
import { CheckoutPaymentStep } from './pages/checkout/CheckoutPaymentStep'
import { CheckoutSuccessStep } from './pages/checkout/CheckoutSuccessStep'
import { CheckoutFailedStep } from './pages/checkout/CheckoutFailedStep'

function App() {
  return (
    <CustomerDomainGuard>
      <LocaleProvider>
      <CustomerThemeProvider>
      <BrowserRouter>
        <TenantBrandingProvider>
          <DashboardDocumentTitle />
        <AuthProvider>
          <ToastProvider>
            <Routes>
              <Route path="checkout" element={<CheckoutLayout />}>
                <Route element={<CheckoutFlow />}>
                  <Route path="login" element={<CheckoutLoginStep />} />
                  <Route path="cart" element={<CheckoutCartStep />} />
                  <Route path="delivery" element={<CheckoutDeliveryStep />} />
                  <Route path="payment" element={<CheckoutPaymentStep />} />
                  <Route path="success" element={<CheckoutSuccessStep />} />
                  <Route path="failed" element={<CheckoutFailedStep />} />
                  <Route index element={<CheckoutCartStep />} />
                </Route>
              </Route>

              <Route element={<GuestRoute />}>
                <Route path="login" element={<LoginPage />} />
              </Route>

              <Route element={<ProtectedRoute />}>
                <Route element={<PageLayout />}>
                  <Route index element={<HomePage />} />
                  <Route path="profile" element={<ProfilePage />} />
                  <Route path="addresses" element={<AddressesPage />} />
                  <Route path="orders" element={<OrdersPage />} />
                  <Route path="favorites" element={<FavoritesPage />} />
                </Route>
              </Route>
            </Routes>
          </ToastProvider>
        </AuthProvider>
        </TenantBrandingProvider>
      </BrowserRouter>
      </CustomerThemeProvider>
      </LocaleProvider>
    </CustomerDomainGuard>
  )
}

export default App
