import {
  Route,
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
} from "react-router-dom";
import { AuthApi } from "./api/AuthApi";
import { ServerApi } from "./api/ServerApi";
import { DeviceRoute } from "./routes/DeviceRoute/DeviceRoute";
import { DevicesRoute } from "./routes/DevicesRoute";
import { HomeRoute } from "./routes/HomeRoute";
import { LoginRoute } from "./routes/LoginRoute";
import { LogsRoute } from "./routes/LogsRoute";
import { NotFoundRoute } from "./routes/NotFoundRoute";
import { PendingDevicesRoute } from "./routes/PendingDevicesRoute";
import { RelaysListRoute } from "./routes/RelaysListRoute";
import { BaseAuthenticatedPage } from "./widgets/BaseAuthenticatedPage";
import { OTARoute } from "./routes/OTARoute";
import { ManagementRoute } from "./routes/ManagementRoute";

export function App() {
  if (!AuthApi.SignedIn && !ServerApi.Config.auth_disabled)
    return <LoginRoute />;

  const router = createBrowserRouter(
    createRoutesFromElements(
      <Route path="*" element={<BaseAuthenticatedPage />}>
        <Route path="" element={<HomeRoute />} />
        <Route path="pending_devices" element={<PendingDevicesRoute />} />
        <Route path="devices" element={<DevicesRoute />} />
        <Route path="dev/:id" element={<DeviceRoute />} />
        <Route path="relays" element={<RelaysListRoute />} />
        <Route path="ota" element={<OTARoute />} />
        <Route path="logs" element={<LogsRoute />} />
        <Route path="management" element={<ManagementRoute />} />
        <Route path="*" element={<NotFoundRoute />} />
      </Route>
    )
  );

  return <RouterProvider router={router} />;
}
