import { useMutation } from "@tanstack/react-query";
import { apiPost } from "../../../../lib/api/methods";
import { PACKAGES_ENDPOINTS } from "../endpoints";
import { errorToast, successToast } from "../../../../utils/toast";
import { SUCCESS_MESSAGES } from "./messages";


type CreatePackageOrder = {
  uuid: string;
  packageName: string;
  credits: number;
  amount: number;
  currency: string;
  status: string;
  createdAt: string;
  paidAt: string;
};

export default function useCreatePackageOrder() {
  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["create-package-order"],
    mutationFn: async (data: { packageId: string }) => {
      const res = await apiPost<CreatePackageOrder>(
        PACKAGES_ENDPOINTS.PACKAGE_ORDER,
        { packageUuid: data.packageId },
      );
      return res.data;
    },
    onSuccess: () => {
      successToast(SUCCESS_MESSAGES.CREATE_ORDER);
    },
    onError: (error: any) => {
      errorToast(error?.response?.data?.message ?? "Something went wrong.");
    },
  });

  return { handleCreatePackageOrder: mutateAsync, loading: isPending };
}
