import { BrowserRouter, Route, Routes } from 'react-router-dom'
import HomePage from './app/feed/pages/HomePage.tsx'
import AuthorPage from './app/feed/pages/AuthorPage.tsx'
import SignupPage from './app/auth/pages/SignupPage.tsx'
import LoginPage from './app/auth/pages/LoginPage.tsx'
import LogoutPage from './app/auth/pages/LogoutPage.tsx'
import UnauthorizedHandler from './app/auth/components/UnauthorizedHandler.tsx'
import AdminPage from './app/admin/pages/AdminPage.tsx'
import SignupCodesManagementPage from './app/admin/pages/subpages/SignupCodesManagementPage.tsx'
import { useRefreshSessionLoop } from './useRefreshSessionLoop.ts'
import { initApp } from './initApp.ts'

const { postService, mediaService, authService } = initApp()

export default function App() {
  useRefreshSessionLoop(authService)

  return (
    <BrowserRouter>
      <UnauthorizedHandler>
        <Routes>
          <Route
            path={'/'}
            element={<HomePage postsService={postService} mediaService={mediaService} />}
          />
          <Route path="/u/:username" element={<AuthorPage postsService={postService} />} />
          <Route path="/login" element={<LoginPage authService={authService} />} />
          <Route path="/logout" element={<LogoutPage authService={authService} />} />
          <Route path="/signup/:code?" element={<SignupPage authService={authService} />} />
          <Route path={'/admin'} element={<AdminPage />}>
            <Route
              path={'codes'}
              element={<SignupCodesManagementPage authService={authService} />}
            />
          </Route>
        </Routes>
      </UnauthorizedHandler>
    </BrowserRouter>
  )
}
