"use client";

import { reportAiChat } from "@/lib/api/labs";
import { ErrorBoundary } from "@sentry/nextjs";
import { ReactBotRoot, type ScriptConfig } from "autoflow-widget-react/next";
import { usePathname } from "next/navigation";
import { isExamContentPage } from "@/lib/utils";

const config: ScriptConfig = {
  src: "",
  apiBase: "https://tidb.ai",
  controlled: false,
  // `trigger: true` is set to hide the chat button and control widget behavior accordingly. Original value is null.
  trigger: true,
  chatEngine: "tidb-labs",
  measurementId: undefined,
};

export default function NewTiDBAIWidget() {
  const pathname = usePathname();
  const isExamPage = isExamContentPage(pathname);

  return (
    <>
      {!isExamPage && (
        <ErrorBoundary showDialog={false}>
          <ReactBotRoot
            scriptConfig={config}
            onNewChat={(id) => {
              reportAiChat(id).catch(() => {});
            }}
            ref={(instance) => {
              window.tidbai = instance;
            }}
          />
        </ErrorBoundary>
      )}
    </>
  );
}
