import { createBrowserHistory } from 'history';
import {
  unstable_HistoryRouter as HistoryRouter,
  Navigate,
  Route,
  Routes,
} from 'react-router-dom';

import { Error401, Error404 } from '@/errors';
import { HelpPage } from '@/scene/home/HelpPage';
import { HomePage } from '@/scene/home/HomePage';
import { SSORoutes } from '@/scene/sso/SSORoutes';
import { useHasRight } from '@/service/session';

import { AddPage } from './home/AddPage';
import { SettingsPage } from './home/SettingsPage';
import { MediaRoutes } from './media/MediaRoutes';
import { OnAirPage } from './onAir/OnAirPage';
import { SeasonRoutes } from './season/SeasonRoutes';
import { SeriesRoutes } from './sso/series/SeriesRoutes';
import { TypeRoutes } from './type/TypesRoutes';

export const AppRoutes = () => {
  const { isReadable } = useHasRight('USER');
  return (
    <HistoryRouter
      // @ts-expect-error
      history={createBrowserHistory({ window })}
      basename="/karideo"
    >
      <Routes>
        {/* Need to keep it in all case, it is the only way to log-in */}
        <Route path="sso/*" element={<SSORoutes />} />
        {isReadable ? (
          <>
            <Route path="/" element={<Navigate to="home" replace />} />
            <Route path="home/*" element={<HomePage />} />
            <Route path="help/*" element={<HelpPage />} />
            <Route path="settings/*" element={<SettingsPage />} />
            <Route path="add/*" element={<AddPage />} />
            <Route path="on-air/*" element={<OnAirPage />} />
            <Route path="Series/*" element={<SeriesRoutes />} />
            <Route path="Season/*" element={<SeasonRoutes />} />
            <Route path="Type/*" element={<TypeRoutes />} />
            <Route path="Media/*" element={<MediaRoutes />} />
            <Route path="*" element={<Error404 />} />
          </>

        ) : (
          <Route path="*" element={<Error401 />} />
        )}
      </Routes>
    </HistoryRouter>
  );
};
