import { Layout } from "@/components/Layout";
import { ChatView } from "@/components/chat/ChatView";
import { ImageView } from "@/components/images/ImageView";
import { TranscriptionView } from "@/components/audio/TranscriptionView";
import { GenerationView } from "@/components/audio/GenerationView";
import { PromptsView } from "@/components/prompts/PromptsView";
import { PromptsFormView } from "../components/prompts/PromptsFormView";
import { AreasView } from "@/components/areas/AreasView";
import { ParecerJuridicoView } from "@/components/parecer-juridico/ParecerJuridicoView";
import { ParecerJuridicoFormView } from "@/components/parecer-juridico/ParecerJuridicoFormView";
import { ParecerJuridicoDetailView } from "@/components/parecer-juridico/ParecerJuridicoDetailView";
import { PromptsProvider } from "@/contexts/PromptsContext";
import { Navigate, Route, Routes, useLocation, useNavigate, Outlet } from "react-router-dom";
import React from "react";
import { GlobalFunctions } from "@/GlobalFunctions";

type TabType = "chat" | "images" | "transcription" | "generation" | "prompts" | "parecerJuridico" | "areas";

const PATH_TO_TAB: Record<string, TabType> = {
  "/codex/bate-papo": "chat",
  "/codex/imagens": "images",
  "/codex/transcricao-audio": "transcription",
  "/codex/geracao-audio": "generation",
  "/codex/parecer-juridico": "parecerJuridico",
  "/codex/areas": "areas",
};

function getActiveTabFromPath(pathname: string): TabType {
  if (pathname.includes("/prompts")) return "prompts";
  if (pathname.includes("/parecer-juridico")) return "parecerJuridico";
  const tab = PATH_TO_TAB[pathname];
  if (tab) return tab;
  return "chat";
}

const TAB_TO_PATH: Record<TabType, string> = {
  chat: "/codex/bate-papo",
  images: "/codex/imagens",
  transcription: "/codex/transcricao-audio",
  generation: "/codex/geracao-audio",
  prompts: "/codex/prompts",
  parecerJuridico: "/codex/parecer-juridico",
  areas: "/codex/areas",
};

function CodexLayout() {
  const location = useLocation();
  const navigate = useNavigate();
  const activeTab = getActiveTabFromPath(location.pathname);

  const onTabChange = (tab: TabType) => {
    navigate(TAB_TO_PATH[tab]);
  };

  return (
    <Layout activeTab={activeTab} onTabChange={onTabChange}>
      <Outlet />
    </Layout>
  );
}

const Index = () => {
  React.useEffect(() => {
    if (!GlobalFunctions.isUsuarioLogado()) window.location.replace(import.meta.env.VITE_BASE_URL_HGTX_CORE);
  }, []);

  return (
    <Routes>
      <Route path="" element={<Navigate to="/404" replace />} />
      <Route path="codex" element={<PromptsProvider><CodexLayout /></PromptsProvider>}>
        <Route index element={<Navigate to="bate-papo" replace />} />
        <Route path="bate-papo" element={<ChatView />} />
        <Route path="imagens" element={<ImageView />} />
        <Route path="transcricao-audio" element={<TranscriptionView />} />
        <Route path="geracao-audio" element={<GenerationView />} />
        <Route path="parecer-juridico" element={<ParecerJuridicoView />} />
        <Route path="parecer-juridico/novo" element={<ParecerJuridicoFormView />} />
        <Route path="parecer-juridico/:id" element={<ParecerJuridicoDetailView />} />
        <Route path="prompts" element={<PromptsView />} />
        <Route path="prompts/novo" element={<PromptsFormView />} />
        <Route path="prompts/:id" element={<PromptsFormView />} />
        <Route path="areas" element={<AreasView />} />
      </Route>
      <Route path="*" element={<Navigate to="/404" replace />} />
    </Routes>
  );
};

export default Index;
