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

export const appointmentsApi = createApi({
    reducerPath: 'appointmentsApi',
    baseQuery: baseQueryWithAuth,
    tagTypes: ['Appointment'],
    endpoints: (builder) => ({
        getAppointments: builder.query({
            query: ({doctor_id, start_date, end_date}) => ({
                url: `/appointments/doctor/${doctor_id}/`,
                params: {start_date, end_date},
            }),
            providesTags: ['Appointment'],
            refetchOnMountOrArgChange: 5,
        }),
        getUpcomingAppointments: builder.query({
            query: (doctor_id) => `/appointments/doctor/${doctor_id}/upcoming/`,
            providesTags: ['Appointment'],
        }),
        getByPatientId: builder.query({
            query: (id) => `/appointments/patient/${id}/`,
            providesTags: ['Appointment'],
            refetchOnMountOrArgChange: 5,
        }),
        createAppointment: builder.mutation({
            query: (data) => ({
                url: '/appointments/',
                method: 'POST',
                body: data,
            }),
            invalidatesTags: ['Appointment'],
        }),
        updateAppointment: builder.mutation({
            query: ({id, data}) => ({
                url: `/appointments/${id}/`,
                method: 'PUT',
                body: data,
            }),
            invalidatesTags: ['Appointment'],
        }),
    }),
});

export const {
    useGetAppointmentsQuery,
    useGetUpcomingAppointmentsQuery,
    useGetByPatientIdQuery,
    useCreateAppointmentMutation,
    useUpdateAppointmentMutation,
} = appointmentsApi;