import { Toaster } from "@/components/ui/toaster";
import { Toaster as Sonner } from "@/components/ui/sonner";
import { TooltipProvider } from "@/components/ui/tooltip";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { BrowserRouter, Routes, Route, Navigate, useLocation } from "react-router-dom";
import { ThemeProvider } from "next-themes";
import Index from "./pages/Index";
import NotFound from "./pages/NotFound";
import Redirect from "./pages/Redirect";
import React from "react";
import IntelligenceIAApp from "./modules/intelligence-ia/App";
import FechamentoHgtxApp from "./modules/fechamento-hgtx/App";

const FECHAMENTO_LEGACY_PREFIX = "/fechamento-hgtx";

/** Redireciona URLs antigas `/fechamento-hgtx/...` para `/fechamento/...` (mesmo sufixo, query e hash). */
function FechamentoLegacyRedirect() {
  const location = useLocation();
  const tail = location.pathname.slice(FECHAMENTO_LEGACY_PREFIX.length);
  const to = `/fechamento${tail}`;
  return <Navigate to={`${to}${location.search}${location.hash}`} replace />;
}

const queryClient = new QueryClient();

const App = () => (
  <QueryClientProvider client={queryClient}>
    <ThemeProvider
      attribute="class"
      defaultTheme="light"
      themes={["light", "dark"]}
      enableSystem={false}
      storageKey="hgtx-codex-theme"
      enableColorScheme
    >
      <TooltipProvider>
        <Toaster />
        <Sonner />
        <BrowserRouter>
          <Routes>
            <Route
              path="/redir/:gsikey"
              element={<Redirect />}
            />

            <Route path="/intelligence-ia/*" element={<IntelligenceIAApp />} />
            <Route path="/fechamento/*" element={<FechamentoHgtxApp />} />
            <Route path="/fechamento-hgtx/*" element={<FechamentoLegacyRedirect />} />

            <Route path="/*" element={<Index />} />
            
            <Route path="*" element={<NotFound />} />
            <Route path="/404" element={<NotFound />} />
          </Routes>
        </BrowserRouter>
      </TooltipProvider>
    </ThemeProvider>
  </QueryClientProvider>
);

export default App;


const HGTXLogin = () =>{

  React.useEffect(() => {
    window.location.replace(import.meta.env.VITE_BASE_URL_HGTX_CORE);
  });

  return(<></>);
}