// Need to use the React-specific entry point to import createApi
import { createApi } from "@reduxjs/toolkit/query/react";
import { baseQuery } from "./token.serivce";

// Define a service using a base URL and expected endpoints
export const bankDepositRequestMaster = createApi({
  reducerPath: "BankDeposit",
  baseQuery: baseQuery,
  tagTypes: ["getBankDeposit"],
  endpoints: (builder) => ({
    // ======[Get All]=====

    getBankDepositMaster: builder.query({
      query: ({ size, search, page }) => {
        let baseURL = `/reversal-transactions/bank-transfer/getAll?search=${search || ""}`

        // Conditionally append page and size parameters if they are defined
        if (page !== undefined && size !== undefined) {
          baseURL += `&page=${page}&size=${size}`;
        }

        return baseURL;
      },
      providesTags: ["getBankDeposit"],
    }),

    approveBankDepositRequest: builder.mutation({
      query: ({ id, data }) => ({
        url: `/reversal-transactions/bank-transfer/approve/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getDepositRequest", "getDepositHistory"],
    }),

    createBankDepositReversalRequest: builder.mutation({
      query: ({ id, data }) => ({
        url: `/reversal-transactions/bank-transfer/create/${id}`,
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getDepositRequest", "getDepositHistory"],
    }),

    rejectbankDepositRequest: builder.mutation({
      query: ({ id, data }) => ({
        url: `/reversal-transactions/bank-transfer/reject/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getDepositRequest", "getDepositHistory"],
    }),
  }),
});

export const {
  useGetBankDepositMasterQuery,
  useApproveBankDepositRequestMutation,
  useRejectbankDepositRequestMutation,
  useCreateBankDepositReversalRequestMutation,
} = bankDepositRequestMaster;
