
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { baseQuery } from "../baseQuery";

export const profileApi = createApi({
    reducerPath: "profileApi",
    baseQuery,

    tagTypes: ["userDetails"],

    endpoints: (builder) => ({

        getUserProfileDetails: builder.query({
            query: (id) => `/website/user/${id}`,
            providesTags: ["userDetails"]
        }),

        updateUserProfileDetails: builder.mutation({
            query: ({ userDetails, userId }) => ({ // keep the name of the variables being passed here same as when calling the mutation hook
                url: `/website/user/${userId}`,
                method: "PUT",
                body: userDetails
            }),
            invalidatesTags: ["userDetails"]
        }),

        getUserPasses: builder.query({
            query: ({ cardMode, sort }) => {
                const params = new URLSearchParams()

                if(cardMode) params.append('cardMode',cardMode);
                if(sort) params.append('sort',sort);

                return `/website/passes/all?${params.toString()}`
            }
        })

    })
});

export const {
    useGetUserProfileDetailsQuery,
    useUpdateUserProfileDetailsMutation,
    useGetUserPassesQuery
} = profileApi;