
// 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 sabAdminMaster = createApi({
  reducerPath: "sabAdminMaster",
  baseQuery: baseQuery,
  tagTypes: ["getSubAdmin", "prePopulate","getSubAdminById"],
  endpoints: (builder) => ({



    // ======[Get All]=====

    getSubAdminMaster: builder.query({
      query: () => `/subadmin/admin/getAll`,
      providesTags: ["getSubAdmin"],
    }),

    // // ========[ Create ]========

    createSubAdmin: builder.mutation({
      query: (data) => ({
        url: `/subadmin/admin/create`,
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getSubAdmin", "prePopulate"],
    }),

    // // ========[Update Sponser]========

    updateSubAdmin: builder.mutation({
      query: ({ data, id }) => ({
        url: `/subadmin/admin/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getSubAdmin","getSubAdminById"],
    }),

    getSubAdminById: builder.query({
      query: (id) => `/subadmin/admin/${id}`,
      providesTags: ["getSubAdminById"],
    }),

    // // ========[Toggle Status]========

    toggleStatus: builder.mutation({
      query: ({ id, data }) => ({
        url: `/subadmin/admin/toggle-role/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getSubAdmin"],
    }),


    // ==========[Delete User] ==========
    deleteUser: builder.mutation({
      query: (id) => ({
        url: `/subadmin/admin/${id}`,
        method: "DELETE",
      }),
      invalidatesTags: ["getSubAdmin"],
    }),
    

  }),
});

// Export hooks for usage in functional components
export const {
  useGetSubAdminMasterQuery,
  useCreateSubAdminMutation,
  useUpdateSubAdminMutation,
  useGetSubAdminByIdQuery,
  useToggleStatusMutation,
  useDeleteUserMutation,
} = sabAdminMaster;
