// 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 fawateerRequest = createApi({
  reducerPath: "fawateerRequest",
  baseQuery: baseQuery,
  tagTypes: ["getFawateerRequest", "getApproveHistory", "getApproveComment", "getRejectComment", "getFawateerMakerRequest"],
  endpoints: (builder) => ({


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


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




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

    // getApproveHistory: builder.query({
    //   query: () => `/fawateer/admin/getAll`,
    //   providesTags: ["getApproveHistory"],
    // }),

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

    approveComment: builder.mutation({
      query: ({ id, data }) => ({
        url: `/fawateer/admin/approve/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getFawateerRequest"],
    }),

    rejectComment: builder.mutation({
      query: ({ id, data }) => ({
        url: `/fawateer/admin/reject/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getFawateerRequest"],
    }),
 
  }),
});

// Export hooks for usage in functional components
export const {
  useGetFawateerRequestQuery,
  useGetApproveHistoryQuery,
  useApproveCommentMutation,
  useRejectCommentMutation,
  useGetFawateerForMakerRequestQuery,
  useGetFawateerInvestorsQuery
} = fawateerRequest;
