// 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 banInvestorDetails = createApi({
  reducerPath: "banInvestorDetails",
  baseQuery: baseQuery,
  tagTypes: ["getBanInvestor", "getUnbanInvestor"],
  endpoints: (builder) => ({

    
    getInvestor: builder.query({
      query: () => `/investorDetails/admin`,
      providesTags: ["getBanInvestor"],
    }),
 

    getUnbanInvestor: builder.query({
      query: ({ page, size, search, userStatus, KYCStatus, country_xid  }) => {
        // Start with the base URL, including searchTerm
        let baseURL = `/investorDetails/admin/getAllUnbanned?search=${search || ""}&userStatus=${userStatus ||""}&KYCStatus=${KYCStatus || ""}&country_xid=${country_xid||""}`;
        
        // Conditionally append kycStatus if it's defined
        if (KYCStatus) {
          baseURL += `&KYCStatus=${KYCStatus}`;
        }
    
        // Conditionally append country if it's defined
        if (country_xid) {
          baseURL += `&country_xid=${country_xid}`;
        }
    
        // Conditionally append page and size parameters if they are defined
        if (page !== undefined && size !== undefined) {
          baseURL += `&page=${page}&size=${size}`;
        }
        
        return baseURL;
      },
      providesTags: ["getUnbanInvestor"],
    }),
    


    getbanInvestor: builder.query({
      query: ({ page, size, search, userStatus, KYCStatus, country_xid  }) => {
        // Start with the base URL, including searchTerm
        let baseURL = `/investorDetails/admin/getAllBanned?search=${search || ""}&userStatus=${userStatus ||""}&KYCStatus=${KYCStatus || ""}&country_xid=${country_xid||""}`;
        
        // Conditionally append kycStatus if it's defined
        if (KYCStatus) {
          baseURL += `&KYCStatus=${KYCStatus}`;
        }
    
        // Conditionally append country if it's defined
        if (country_xid) {
          baseURL += `&country_xid=${country_xid}`;
        }
    
        // Conditionally append page and size parameters if they are defined
        if (page !== undefined && size !== undefined) {
          baseURL += `&page=${page}&size=${size}`;
        }
        
        return baseURL;
      },
      providesTags: ["getBanInvestor"],
    }),

    updateUnban: builder.mutation({
      query: ({ id, data }) => ({
        url: `/investorDetails/admin/banById/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getBanInvestor","getUnbanInvestor"],
    }),


    updateBan: builder.mutation({
      query: ({ id, data }) => ({
        url: `/investorDetails/admin/unBanById/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getBanInvestor", "getUnbanInvestor"],
    }),

  }),
});

// Export hooks for usage in functional components
export const {
  useGetUnbanInvestorQuery,
  useGetInvestorQuery,
  useGetbanInvestorQuery,
  useUpdateUnbanMutation,
  useUpdateBanMutation
} = banInvestorDetails;
