import { 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,
    } = useGetPatientsQuery(undefined, {
        pollingInterval: 20000,
    });
    const {
        data: appointmentTypes = [],
        isLoading: isLoadingAppointmentTypes,
        isError: isErrorAppointmentTypes,
    } = useGetAppointmentTypesQuery(undefined, {
        pollingInterval: 20000,
    });

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

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

export default useAppointmentFormModal;