import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import CONFIG from "../Core/сonfig.js";

export const scheduledAppointmentsApi = createApi({
    reducerPath: 'scheduledAppointmentsApi',
    baseQuery: fetchBaseQuery({
        baseUrl: CONFIG.BASE_URL,
        prepareHeaders: (headers) => {
            const token = localStorage.getItem('access_token');
            if (token) headers.set('Authorization', `Bearer ${token}`);
            return headers;
        },
    }),
    tagTypes: ['ScheduledAppointment'],
    endpoints: (builder) => ({
        getScheduledAppointments: builder.query({
            query: () => `/scheduled_appointments/`,
            providesTags: ['ScheduledAppointment'],
        }),
        createScheduledAppointment: builder.mutation({
            query: (data) => ({
                url: '/scheduled_appointments/',
                method: 'POST',
                body: data,
            }),
            invalidatesTags: ['ScheduledAppointment'],
        }),
        updateScheduledAppointment: builder.mutation({
            query: ({ id, data }) => ({
                url: `/scheduled_appointments/${id}/`,
                method: 'PUT',
                body: data,
            }),
            invalidatesTags: ['ScheduledAppointment'],
        }),
        cancelScheduledAppointment: builder.mutation({
            query: (id) => ({
                url: `/scheduled_appointments/${id}/cancel/`,
                method: 'POST',
            }),
            invalidatesTags: ['ScheduledAppointment'],
        }),
    }),
});

export const {
    useGetScheduledAppointmentsQuery,
    useCreateScheduledAppointmentMutation,
    useUpdateScheduledAppointmentMutation,
    useCancelScheduledAppointmentMutation,
} = scheduledAppointmentsApi;