import {useGetAllPatientsQuery, useGetPatientsQuery} from "../../../Api/patientsApi.js";
import {useGetAppointmentTypesQuery} from "../../../Api/appointmentTypesApi.js";
import {
    useCreateAppointmentMutation,
    useGetByPatientIdQuery,
} from "../../../Api/appointmentsApi.js";
import {useCancelScheduledAppointmentMutation} from "../../../Api/scheduledAppointmentsApi.js";

const useAppointmentFormModal = () => {
    const {
        data: patients = [],
        isLoading: isLoadingPatients,
        isError: isErrorPatients,
    } = useGetAllPatientsQuery(undefined);
    const {
        data: appointmentTypes = [],
        isLoading: isLoadingAppointmentTypes,
        isError: isErrorAppointmentTypes,
    } = useGetAppointmentTypesQuery(undefined);

    const [createAppointment, {isLoading: isCreating, isError: isErrorCreating}] = useCreateAppointmentMutation();
    const [cancelAppointment, {
        isLoading: isCanceling,
        isError: isErrorCanceling
    }] = useCancelScheduledAppointmentMutation();

    return {
        patients,
        appointmentTypes,
        createAppointment,
        cancelAppointment,
        useGetByPatientIdQuery,
        isLoading: isLoadingPatients || isLoadingAppointmentTypes || isCreating || isCanceling,
        isError: isErrorPatients || isErrorAppointmentTypes || isErrorCreating || isErrorCanceling,
        isProcessed: isCreating || isCanceling,
    };
};

export default useAppointmentFormModal;