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


export const backupsApi = createApi({
    reducerPath: 'backupsApi',
    baseQuery: baseQueryWithAuth,
    tagTypes: ['Backup'],
    endpoints: (builder) => ({
        getBackups: builder.query({
            query: () => `/backups/`,
            providesTags: ['Backup'],
        }),
        createBackup: builder.mutation({
            query: () => ({
                url: '/backups/',
                method: 'POST',
            }),
            invalidatesTags: ['Backup'],
        }),
        deleteBackup: builder.mutation({
            query: (backupId) => ({
                url: `/backups/${backupId}/`,
                method: 'DELETE',
            }),
            invalidatesTags: ['Backup'],
        }),
        uploadBackup: builder.mutation({
            query: (file) => {
                const formData = new FormData();
                formData.append('file', file);

                return {
                    url: '/backups/upload/',
                    method: 'POST',
                    credentials: 'include',
                    formData: true,
                    body: formData,
                };
            },
            invalidatesTags: ['Backup'],
        }),
    }),
});

export const {
    useGetBackupsQuery,
    useCreateBackupMutation,
    useDeleteBackupMutation,
    useUploadBackupMutation,
} = backupsApi;
