import { toast } from "@core/hooks/useToast.ts";
import { useActiveClient } from "@meshtastic/sdk-react";
import { useTranslation } from "react-i18next";
import { DialogWrapper } from "../DialogWrapper.tsx";

export interface ResetNodeDbDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
}

export const ResetNodeDbDialog = ({
  open,
  onOpenChange,
}: ResetNodeDbDialogProps) => {
  const { t } = useTranslation("dialog");
  const meshClient = useActiveClient();

  const handleResetNodeDb = () => {
    if (!meshClient) return;
    meshClient.nodes
      .reset({ keepMyNode: true })
      .then((result) => {
        if (result.status === "error") throw result.error;
        return meshClient.chat.clearAll();
      })
      .catch((error) => {
        toast({ title: t("resetNodeDb.failedTitle") });
        console.error("Failed to reset Node DB:", error);
      });
  };

  return (
    <DialogWrapper
      open={open}
      onOpenChange={onOpenChange}
      type="confirm"
      variant="destructive"
      title={t("resetNodeDb.title")}
      description={t("resetNodeDb.description")}
      confirmText={t("resetNodeDb.confirm")}
      onConfirm={handleResetNodeDb}
    />
  );
};
