import { BrowserRouter, Routes, Route } from 'react-router-dom'
import { WagmiProvider } from 'wagmi'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ThirdwebProvider } from '@thirdweb-dev/react'
import { config } from './config/wagmi'
import { AdminProvider } from './contexts/AdminContext'
import { ErrorBoundary } from './components/ErrorBoundary'
import BridgePage from './pages/BridgePage'
import SwapPage from './pages/SwapPage'
import TradePage from './pages/TradePage'
import CentralBankPage from './pages/CentralBankPage'
import Office24Page from './pages/Office24Page'
import ReservePage from './pages/ReservePage'
import HistoryPage from './pages/HistoryPage'
import AdminPanel from './pages/AdminPanel'
import DocsPage from './pages/DocsPage'
import WalletsDemoPage from './pages/WalletsDemoPage'
import Layout from './components/layout/Layout'
import OmnlProductLayout from './components/layout/OmnlProductLayout'
import ToastProvider from './components/ui/ToastProvider'

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      retry: (failureCount, error: unknown) => {
        const err = error as { code?: string; message?: string }
        if (err?.code === 'CALL_EXCEPTION' || err?.message?.includes('call revert exception')) {
          return false
        }
        return failureCount < 2
      },
    },
  },
})
const THIRDWEB_CLIENT_ID = import.meta.env.VITE_THIRDWEB_CLIENT_ID || '542981292d51ec610388ba8985f027d7'

function App() {
  return (
    <ErrorBoundary>
      <WagmiProvider config={config}>
        <QueryClientProvider client={queryClient}>
          <BrowserRouter
            future={{
              v7_startTransition: true,
              v7_relativeSplatPath: true,
            }}
          >
            <ToastProvider />
            <AdminProvider>
              <Routes>
                <Route element={<OmnlProductLayout />}>
                  <Route path="/central-bank" element={<CentralBankPage />} />
                  <Route path="/office-24" element={<Office24Page />} />
                  <Route path="/trade" element={<TradePage />} />
                  <Route path="/swap" element={<SwapPage />} />
                </Route>
                <Route
                  element={
                    <ThirdwebProvider clientId={THIRDWEB_CLIENT_ID}>
                      <Layout />
                    </ThirdwebProvider>
                  }
                >
                  <Route path="/" element={<BridgePage />} />
                  <Route path="/reserve" element={<ReservePage />} />
                  <Route path="/history" element={<HistoryPage />} />
                  <Route path="/admin" element={<AdminPanel />} />
                  <Route path="/docs" element={<DocsPage />} />
                  <Route path="/wallets" element={<WalletsDemoPage />} />
                </Route>
              </Routes>
            </AdminProvider>
          </BrowserRouter>
        </QueryClientProvider>
      </WagmiProvider>
    </ErrorBoundary>
  )
}

export default App
