// investorDetails.service.js
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
// import { api } from "./api.service";
import { baseQuery } from "./token.serivce";

// const baseUrl = api?.defaults.baseURL;

// Define a service using a base URL and expected endpoints
export const drawalRequest = createApi({
  reducerPath: "drawalRequest",
  baseQuery: baseQuery,
  tagTypes: ["getDrawalRequest", "getDepositHistory"],
  endpoints: (builder) => ({




    getDrawalRequest: builder.query({
      query: ({ page, size, search }) => {
        let baseURL = `/withdrawal/admin?search=${search || ""}`;
        if (page !== undefined && size !== undefined) {
          baseURL += `&page=${page}&size=${size}`; // Only add pagination if both are defined
        }
        return baseURL;
      },
      providesTags: ["getDrawalRequest"],
    }),

    getDrawalRequestById: builder.query({
      query: (id) => `/withdrawal/admin/getById/${id}`,
    }),

    updateDrawalRequest: builder.mutation({
      query: ({ id, data }) => ({
        url: `/withdrawal/admin/updateApprove/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getDrawalRequest", "getDepositHistory"],
    }),

    depositReject: builder.mutation({
      query: ({ id, data }) => ({
        url: `/deposit/admin/rejected/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getDepositRequest", "getDepositHistory"],
    }),


    getDrawalHistory: builder.query({
      query: ({ page, size, search, userStatus, KYCStatus, country_xid  }) => {
        // Start with the base URL, including searchTerm
        let baseURL = `/withdrawal/admin/history?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: ["getDepositHistory"],
    }),


  }),
});

// Export hooks for usage in functional components
export const {
  useGetDrawalRequestQuery,
  useUpdateDrawalRequestMutation,
  useGetDrawalRequestByIdQuery,
  useDepositRejectMutation,
  useGetDrawalHistoryQuery
} = drawalRequest;
