// io.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 ioService = createApi({
  reducerPath: "ioService",
  baseQuery: baseQuery,
  tagTypes: [
    "prePopulate",
    "getIO",
    "getKeyMerits",
    "getArtifactsVideo",
    "getInvestmentDocuments",
    "getIOById",
    "getSponser", 
    "getInvestmentType", 
    "getInvestmentTypeID"
  ],
  endpoints: (builder) => ({
    // =====[get prepopulate data]
    getIOprepopulateData: builder.query({
      query: () => `/io/admin/pre-populate`,
      providesTags: ["prePopulate"],
    }),

    // =====[get]
    getIOs: builder.query({
      query: ({ page, size }) => `/io/admin?page=${page}&size=${size}`,
      providesTags: ["getIO"],
    }),

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

    // =====[create]
    createIO: builder.mutation({
      query: (data) => ({
        url: `/io/admin`,
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getIO"],
    }),

    updateIO: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getIOById", "getIO", "prePopulate"],
    }),

    // =====[Key Merits]
    getKeyMerits: builder.query({
      query: (id) => `/io/admin/key-merits/${id}`,
      providesTags: ["getKeyMerits"],
    }),

    createKeyMerits: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/key-merits/${id}`,
        method: "POST",
        body: data,
        // No need to manually set 'Content-Type'
      }),
      invalidatesTags: ["getIOById"],
    }),

    deleteKeyMerits: builder.mutation({
      query: (id) => ({
        url: `/io/admin/key-merits/hard-delete/${id}`,
        method: "DELETE",
      }),
      invalidatesTags: ["getIOById"],
    }),

    updateKeyMerits: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/key-merits/byId/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

    // =====[getIODocument]
    createInvestmentDocuments: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/document/${id}`,
        method: "POST",
        body: data,
      }),

      invalidatesTags: ["getIOById"],
    }),

    updateInvestmentDocuments: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/document/byId/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

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

    deleteIODocs: builder.mutation({
      query: (id) => ({
        url: `/io/admin/document/hard-delete/${id}`,
        method: "DELETE",
      }),
      invalidatesTags: ["getIOById"],
    }),

    // =====[Artifacts]
    getArtifactsVideo: builder.query({
      query: (id) => `/io/artifact/artifactVideo/${id}`,
      providesTags: ["getArtifactsVideo"],
    }),

    // =====[createImageArtifacts]
    createImageArtifacts: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/artifact/image/${id}`,
        method: "POST",
        body: data,
      }),

      invalidatesTags: ["getIOById"],
    }),

    updateImageArtifacts: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/artifact/image/byId/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

    // =====[createVideoArtifacts]
    createVideoArtifacts: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/artifact/video/${id}`,
        method: "POST",
        body: data,
      }),

      invalidatesTags: ["getIOById"],
    }),

    deleteVideoArtifacts: builder.mutation({
      query: (id) => ({
        url: `/io/admin/artifact/video/byId/${id}`,
        method: "DELETE",
      }),
      invalidatesTags: ["getIOById"],
    }),

    deleteImageArtifacts: builder.mutation({
      query: (id) => ({
        url: `/io/admin/artifact/image/byId/${id}`,
        method: "DELETE",
      }),
      invalidatesTags: ["getIOById"],
    }),

    updateVideoArtifacts: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/artifact/video/byId/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

    updateStatusIo: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/transaction/${id}/update-status/`,
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getIOById", "getIO"],
    }),

    createIoCash: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/transaction/${id}/io-cash/`,
        method: "POST",
        body: data,
      }),

      invalidatesTags: ["getIOById"],
    }),

    createIoNav: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/transaction/${id}/io-nav/`,
        method: "POST",
        body: data,
      }),

      invalidatesTags: ["getIOById"],
    }),

    // =====[ Amount Investment ] ======
    amountIvestment: builder.mutation({
      query: ({ data, id }) => ({
        url: `/io/admin/transaction/${id}/amount-invested`,

        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

    // ======== [ Distribution Transaction ] ========

    getDistributionInvestor: builder.mutation({
      query: ({ id, data }) => ({
        url: `/io/admin/transaction/${id}/calculate-distribution-amt`,
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

    getDistributedToInvestor: builder.mutation({
      query: ({ id, data }) => ({
        url: `/io/admin/transaction/${id}/distributed-to-investor`,
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

    updateExitToInvestor: builder.mutation({
      query: ({ id, data }) => ({
        url: `/io/admin/transaction/${id}/exit`,
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),



// ==============[ Displaye Orders ]===============

    setDisplayOrder: builder.mutation({
      query: ({ data }) => ({
        url: `/io/admin/key-merits/resetDisplayOrder`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

    setDisplayOrderIODocuments: builder.mutation({
      query: ({ data }) => ({
        url: `/io/admin/document/resetDisplayOrder`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

    setDisplayOrderIOArtifactsImage: builder.mutation({
      query: ({ data }) => ({
        url: `/io/admin/artifact/image/resetDisplayOrder`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

    setDisplayOrderIOArtifactsVideo: builder.mutation({
      query: ({ data }) => ({
        url: `/io/admin/artifact/video/resetDisplayOrder`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getIOById"],
    }),

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

    createSponser: builder.mutation({
      query: (data) => ({
        url: `/sponsor/admin`,
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getSponser","prePopulate"],
    }),

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

    updateSponser: builder.mutation({
      query: ({ data, id }) => ({
        url: `/sponsor/admin/${id}`,
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getSponser","prePopulate"],
    }),

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

    getSponserMaster: builder.query({
      query: ({ page, size }) => `/sponsor/admin?page=${page}&size=${size}`,
      providesTags: ["getSponser"],
    }),

    // ========[Delete Sponser]========

     deleteSponser: builder.mutation({
      query: (id) => ({
        url: `/sponsor/admin/delete/${id}`,
        method: "DELETE",
      }),
      invalidatesTags: ["getSponser"],
    }),

    // ========[Get Active]========

    getActiveSponserMaster: builder.query({
      query: () => `/sponsor/admin/active`,
    }),

    getSponserMasterActive: builder.query({
      query: () => "/sponsor/admin/active",
    }),
    
    // ======[Get ID]=====

    getSponserById: builder.query({
      query: (id) => `/sponsor/admin/${id}`,
    }),

    // ========[Get Active]========

    getActiveSponserMaster: builder.query({
      query: () => `/sponsor/admin/active`,
    }),




// ===============================[ Investment Type ]===================================

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

    getInvestmentTypes: builder.query({
      query: ({ page, size }) =>
        `/investmentType/admin?page=${page}&size=${size}`,
      providesTags: ["getInvestmentType"],
    }),

    // ========[Get ID]=========

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

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

    createInvestmentType: builder.mutation({
      query: (data) => ({
        url: `/investmentType/admin/`,
        method: "POST",
        body: data,
      }),
      invalidatesTags: ["getInvestmentType", "prePopulate"],
    }),

    // ========[Update Investment]=======

    updateInvestmentType: builder.mutation({
      query: ({ data, id }) => ({
        url: `/investmentType/admin/${id}`, 
        method: "PATCH",
        body: data,
      }),
      invalidatesTags: ["getInvestmentTypeID", "getInvestmentType", "prePopulate"],
    }),

    // ========[Delete Investment]=======

    deleteInvestmentType: builder.mutation({
      query: (id) => ({
        url: `/investmentType/admin/delete/${id}`,
        method: "DELETE",
      }),
      invalidatesTags: ["getInvestmentType", 'prePopulate'],
    }),















  }),
});

// Export hooks for usage in functional components
export const {
  useGetIOprepopulateDataQuery,

  useGetIOsQuery,
  useGetIOByIdQuery,
  useCreateIOMutation,
  useUpdateIOMutation,

  useGetKeyMeritsQuery,
  useCreateKeyMeritsMutation,
  useDeleteKeyMeritsMutation,
  useUpdateKeyMeritsMutation,

  useGetInvestmentDocumentsQuery,
  useCreateInvestmentDocumentsMutation,
  useDeleteIODocsMutation,
  useUpdateInvestmentDocumentsMutation,

  useCreateImageArtifactsMutation,
  useUpdateImageArtifactsMutation,
  useUpdateVideoArtifactsMutation,

  useGetArtifactsVideoQuery,
  useCreateVideoArtifactsMutation,
  useDeleteVideoArtifactsMutation,
  useDeleteImageArtifactsMutation,


  useSetDisplayOrderMutation,
  useSetDisplayOrderIODocumentsMutation,
  useSetDisplayOrderIOArtifactsImageMutation,
  useSetDisplayOrderIOArtifactsVideoMutation,

  useCreateIoCashMutation,
  useCreateIoNavMutation,

  useUpdateStatusIoMutation,

  useAmountIvestmentMutation,

  useGetDistributionInvestorMutation,

  useGetDistributedToInvestorMutation,
  useUpdateExitToInvestorMutation,


// ==============[ Sponser ]===============
  useGetSponserMasterQuery,
  useGetSponserMasterActiveQuery,
  useCreateSponserMutation,
  useUpdateSponserMutation,
  useGetSponserByIdQuery,
  useDeleteSponserMutation,
  useGetActiveSponserMasterQuery,


  // ============[ Investment Type ]========


  useGetInvestmentTypesQuery,
  useGetInvestmentTypeByIdQuery,
  useCreateInvestmentTypeMutation,
  useUpdateInvestmentTypeMutation,
  useDeleteInvestmentTypeMutation,

} = ioService;
