// 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 reversalFawateerDepositMaster = createApi({
  reducerPath: "FawateerDeposit",
  baseQuery: baseQuery,
  tagTypes: ["getFawateerDeposit", "getDepositHistory"],
  endpoints: (builder) => ({
    // ======[Get All]=====

    getFawateerDepositMaster: builder.query({
      query: ({ search, page, size }) => {
        let baseURL = `/reversal-transactions/fawateer/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: ["getFawateerDeposit"],
    }),

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

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

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

export const {
  useGetFawateerDepositMasterQuery,
  useApproveFawateerRequestMutation,
  useCreateFawateerReversalRequestMutation,
  useRejectFawateerRequestMutation,
} = reversalFawateerDepositMaster;
