"use client";
import React from "react";
import {Button} from "./ui/button";
import {Trash} from "lucide-react";
import privateApi from "@/service/http/privateCall.axios";
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "./ui/dialog";
import {toast} from "react-toastify";
import {handleAxiosError} from "@/lib/utils";
import { ServerResponseObject } from "@/types";

async function deleteUser(id: number | string, route: string) {
  try {
    const {message} = await privateApi.delete(`/${route}/delete/${id}`) as ServerResponseObject;
    toast.success(message);
  } catch (error) {
    toast.error(handleAxiosError(error));
  }
}
export default function DeleteExpertiseButton({
  id,
  route,
}: {
  id: number | string;
  route: string;
}) {

  const handleDelete = async () => {
    await deleteUser(id, route);
  };
  return (
    <>
      <Dialog>
        <DialogTrigger asChild>
          <Button variant="destructive">
            <Trash />
          </Button>
        </DialogTrigger>
        <DialogContent className="sm:max-w-[425px]" dir="rtl">
          <DialogHeader>
            <DialogTitle>حذف آیتم</DialogTitle>
            <DialogDescription>
              آیا از حذف این آیتم اطمینان دارد ؟
            </DialogDescription>
          </DialogHeader>

          <DialogFooter>
            <DialogClose asChild>
              <Button variant="outline">انصراف</Button>
            </DialogClose>
            <Button onClick={handleDelete} type="submit">
              بله
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </>
  );
}
