import {createApi} from "@reduxjs/toolkit/query/react";
import {baseQueryWithAuth} from "./baseQuery.js";


export const usersApi = createApi({
    reducerPath: 'usersApi',
    baseQuery: baseQueryWithAuth,
    tagTypes: ['User'],
    endpoints: (builder) => ({
        getAllUsers: builder.query({
            query: () => '/users/',
            providesTags: ['User'],
            refetchOnMountOrArgChange: 5,
        }),
        getAuthenticatedUserData: builder.query({
            query: () => '/users/my-data/',
            providesTags: ['User'],
            refetchOnMountOrArgChange: 5,
        }),
        changePassword: builder.mutation({
            query: (data) => ({
                url: "/users/change-password/",
                method: "POST",
                body: data,
            }),
        }),
        updateUser: builder.mutation({
            query: ({userId, ...data}) => ({
                url: `/users/${userId}/`,
                method: "PUT",
                body: data,
            }),
            invalidatesTags: ['User']
        }),
    }),
});

export const {
    useGetAuthenticatedUserDataQuery,
    useChangePasswordMutation,
    useUpdateUserMutation,
    useGetAllUsersQuery,
} = usersApi;