import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom'
import { HomePage } from '@pages/home'
import { WalletPage } from '@pages/wallet'
import { SwapPage } from '@pages/swap'
import { BridgePage } from '@pages/bridge'
import { ProfilePage, ProfileDetailsItem, SecurityItem, MnemonicItem, TransactionsItem } from '@pages/profile'
import { LoginPage } from '@pages/login'
import { RegisterPage } from '@pages/register'
import { RegisterTestPage } from '@pages/register-test'
import { ConverterPage } from '@pages/converter'
import { SeedPhrasePage } from '@pages/seed-phrase'
import { KycPage } from '@pages/kyc'
import { RestorePasswordPage } from '@pages/restore-password'
import { PublichnayaOfertaPage } from '@pages/publichnaya-oferta'
import { PolitikaPage } from '@pages/politika-personalnyh-dannyh'
import { PolitikaCookiePage } from '@pages/politika-cookie'
import { SoglasiePage } from '@pages/soglasie-personalnyh-dannyh'
import { ReestryPage } from '@pages/reestr-pd-rkn'
import { StakingPage } from '@pages/staking'
import { PoolsPage } from '@pages/pools'
import { NotFoundPage } from '@pages/not-found'
import { WalletLayout } from '@widgets/wallet-layout'
import { ROUTES } from '@shared/config/routes'
import { ScrollToTop } from './ScrollToTop'
import { ProtectedRoute } from './ProtectedRoute'
import { GuestRoute } from './GuestRoute'

export function RouterProvider() {
  return (
    <BrowserRouter>
      <ScrollToTop />
      <Routes>
        <Route path={ROUTES.HOME} element={<HomePage />} />
        <Route path={ROUTES.PUBLICHNAYA_OFERTA} element={<PublichnayaOfertaPage />} />
        <Route path={ROUTES.POLITIKA_PERSONALNYH_DANNYH} element={<PolitikaPage />} />
        <Route path={ROUTES.POLITIKA_COOKIE} element={<PolitikaCookiePage />} />
        <Route path={ROUTES.SOGLASIE_PERSONALNYH_DANNYH} element={<SoglasiePage />} />
        <Route path={ROUTES.REESTR_PD_RKN} element={<ReestryPage />} />
        <Route path={ROUTES.REGISTER_TEST} element={<RegisterTestPage />} />

        <Route element={<GuestRoute />}>
          <Route path={ROUTES.LOGIN} element={<LoginPage />} />
          <Route path={ROUTES.REGISTER} element={<RegisterPage />} />
          <Route path={ROUTES.RESTORE_PASSWORD} element={<RestorePasswordPage />} />
        </Route>

        <Route element={<ProtectedRoute />}>
          <Route element={<WalletLayout footer center />}>
            <Route path={ROUTES.CONVERTER} element={<ConverterPage />} />
          </Route>

          <Route element={<WalletLayout footer />}>
            <Route path={ROUTES.SWAP} element={<SwapPage />} />
            <Route path={ROUTES.BRIDGE} element={<BridgePage />} />
            <Route path={ROUTES.STAKING} element={<StakingPage />} />
            <Route path={ROUTES.POOLS} element={<PoolsPage />} />

            {/* Profile dashboard: WalletLayout gives header/footer, ProfilePage
                renders the menu + an inner <Outlet/> for the active item. */}
            <Route path={ROUTES.PROFILE} element={<ProfilePage />}>
              <Route index element={<Navigate to="details" replace />} />
              <Route path="details" element={<ProfileDetailsItem />} />
              <Route path="security" element={<SecurityItem />} />
              <Route path="mnemonic" element={<MnemonicItem />} />
              <Route path="transactions" element={<TransactionsItem />} />
            </Route>
          </Route>

          {/* Legacy route — transactions now live inside the profile dashboard. */}
          <Route path={ROUTES.TRANSACTIONS} element={<Navigate to={ROUTES.PROFILE_TRANSACTIONS} replace />} />

          <Route path={ROUTES.WALLET} element={<WalletPage />} />
          <Route path={ROUTES.WALLET_CHAIN} element={<WalletPage />} />
          <Route path={ROUTES.SEED_PHRASE} element={<SeedPhrasePage />} />
          <Route path={ROUTES.KYC} element={<KycPage />} />
        </Route>

        <Route path="*" element={<NotFoundPage />} />
      </Routes>
    </BrowserRouter>
  )
}
