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

export const scheduledAppointmentsApi = createApi({
    reducerPath: 'scheduledAppointmentsApi',
    baseQuery: baseQueryWithAuth,
    tagTypes: ['ScheduledAppointment'],
    endpoints: (builder) => ({
        getScheduledAppointments: builder.query({
            query: ({ doctor_id, start_date, end_date }) => ({
                url: `/scheduled_appointments/doctor/${doctor_id}/`,
                params: { start_date, end_date },
            }),
            providesTags: ['ScheduledAppointment'],
        }),
        getUpcomingScheduledAppointments: builder.query({
            query: (doctor_id) => `/scheduled_appointments/doctor/${doctor_id}/upcoming/`,
            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,
    useGetUpcomingScheduledAppointmentsQuery,
    useCreateScheduledAppointmentMutation,
    useUpdateScheduledAppointmentMutation,
    useCancelScheduledAppointmentMutation,
} = scheduledAppointmentsApi;