import { useEffect } from "react";
import { UserAuth } from "../../context/AuthContent";
import AyudaIaDrawer from "./AyudaIaDrawer";
import { loadScreenMap } from "./screenKeyMap";
import { useAyudaIaStore } from "./ayudaIaStore";

/**
 * Monta el panel de NovaIA y el atajo Ctrl + /. No dibuja ningún botón: los
 * accesos viven en el menú lateral, en el header de celular y en `ScreenGuia`,
 * así nunca tapan contenido (antes era un FAB fijo abajo a la derecha que
 * pisaba paginaciones y barras de acciones).
 */
export default function AyudaIaHost() {
  const { isAuthenticated } = UserAuth() || {};
  const abierto = useAyudaIaStore((s) => s.abierto);
  const cerrar = useAyudaIaStore((s) => s.cerrar);

  useEffect(() => {
    if (!isAuthenticated) return;
    loadScreenMap();
  }, [isAuthenticated]);

  useEffect(() => {
    if (!isAuthenticated) return undefined;
    const onKey = (e) => {
      if ((e.ctrlKey || e.metaKey) && (e.key === "?" || e.key === "/")) {
        e.preventDefault();
        useAyudaIaStore.getState().alternar();
      }
    };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, [isAuthenticated]);

  if (!isAuthenticated) return null;
  return <AyudaIaDrawer open={abierto} onClose={cerrar} />;
}
