import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import * as api from "../service/ReceiptService";
import {
  ApplyDiscountType,
  ApproveInvoiceType,
  RemoveDiscountType,
  RequestApproveInvoiceType,
} from "../types/ReceiptTypes";

export const useGetInvoices = (status: string) => {
  return useQuery({
    queryKey: ["invoices", status],
    queryFn: () => api.getInvoice(status),
  });
};

export const useGetInvoiceDetail = (id: string) => {
  return useQuery({
    queryKey: ["invoice", id],
    queryFn: () => api.getInvoiceDetail(id),
    enabled: !!id,
  });
};

export const useApproveInvoice = () => {
  return useMutation({
    mutationFn: (variables: ApproveInvoiceType) =>
      api.approveInvoice(variables),
  });
};

export const usePayInvoice = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (variables: string) => api.payInvoice(variables),
    onSuccess: () => {
      queryClient.refetchQueries({
        queryKey: ["wallet-balance"],
      });
    },
  });
};

export const useRequestApprove = () => {
  return useMutation({
    mutationFn: (variables: RequestApproveInvoiceType) =>
      api.requestApprove(variables),
  });
};

export const useApplyDiscount = () => {
  return useMutation({
    mutationFn: (variables: ApplyDiscountType) => api.applyDiscount(variables),
  });
};

export const useRemoveDiscount = () => {
  return useMutation({
    mutationFn: (variables: RemoveDiscountType) =>
      api.removeDiscount(variables.id),
  });
};
