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

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

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

    approveAccountDeletionRequest: builder.mutation({
      query: ({ id, data }) => ({
        url: `/reversal-transactions/account-deletion/approve/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getAccountDeletion", "getHistory"],
    }),

    createAccountDeletionReversalRequest: builder.mutation({
      query: ({ id, data }) => ({
        url: `/reversal-transactions/account-deletion/create/${id}`,
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getAccountDeletion", "getHistory"],
    }),

    rejectAccountDeletionRequest: builder.mutation({
      query: ({ id, data }) => ({
        url: `/reversal-transactions/account-deletion/reject/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getAccountDeletion", "getHistory"],
    }),
  }),
});

export const {
  useGetAccountDeletionMasterQuery,
  useCreateAccountDeletionReversalRequestMutation,
  useApproveAccountDeletionRequestMutation,
  useRejectAccountDeletionRequestMutation
} = reversalAccountDeletionMaster;
