import React from "react";
import { BrowserRouter } from "react-router-dom";
import { AuthProvider } from "react-oidc-context";
import { UserProvider } from "../Contexts/UserContext";
import { StatusProvider } from "../Contexts/StatusContext";
import { CssBaseline } from "@material-ui/core";
import MenuDrawer from "../Layouts/MenuDrawer";
import SiteRoutes from "../Routes/SiteRoutes";
import { } from "../../services/monitoring";
import { keycloakConfig, isKeycloakEnabled } from "../../services/keycloak";

function AppContent() {
  return (
    <StatusProvider>
      <UserProvider>
        <CssBaseline />
        <BrowserRouter>
          <MenuDrawer>
            <SiteRoutes />
          </MenuDrawer>
        </BrowserRouter>
      </UserProvider>
    </StatusProvider>
  );
}

const onSigninCallback = () => {
  // Remove OIDC params from URL after signin
  window.history.replaceState({}, document.title, window.location.pathname);
};

function App() {
  // Only wrap with AuthProvider if Keycloak is enabled
  if (isKeycloakEnabled()) {
    return (
      <AuthProvider 
        {...keycloakConfig}
        onSigninCallback={onSigninCallback}
      >
        <AppContent />
      </AuthProvider>
    );
  }
  
  return <AppContent />;
}

export default App;
