import { useGetAppointmentsQuery } from "../../../Api/appointmentsApi.js";
import { useGetScheduledAppointmentsQuery } from "../../../Api/scheduledAppointmentsApi.js";
import { useGetPatientsQuery } from "../../../Api/patientsApi.js";
import { notification } from "antd";
import { useEffect } from "react";

const useAppointments = () => {
    const {
        data: appointments = [],
        isLoading: isLoadingAppointments,
        isError: isErrorAppointments,
    } = useGetAppointmentsQuery(undefined, {
        pollingInterval: 20000,
    });

    const {
        data: scheduledAppointments = [],
        isLoading: isLoadingScheduledAppointments,
        isError: isErrorScheduledAppointments,
    } = useGetScheduledAppointmentsQuery(undefined, {
        pollingInterval: 20000,
    });

    const {
        data: patients = [],
        isLoading: isLoadingPatients,
        isError: isErrorPatients,
    } = useGetPatientsQuery(undefined, {
        pollingInterval: 20000,
    });

    useEffect(() => {
        if (isErrorAppointments) {
            notification.error({
                message: 'Ошибка',
                description: 'Ошибка загрузки приемов.',
                placement: 'topRight',
            });
        }
        if (isErrorScheduledAppointments) {
            notification.error({
                message: 'Ошибка',
                description: 'Ошибка загрузки запланированных приемов.',
                placement: 'topRight',
            });
        }
        if (isErrorPatients) {
            notification.error({
                message: 'Ошибка',
                description: 'Ошибка загрузки пациентов.',
                placement: 'topRight',
            });
        }
    }, [isErrorAppointments, isErrorScheduledAppointments, isErrorPatients]);

    return {
        patients,
        appointments,
        scheduledAppointments,
        isLoading: isLoadingAppointments || isLoadingScheduledAppointments || isLoadingPatients,
        isError: isErrorAppointments || isErrorScheduledAppointments || isErrorPatients,
    };
};

export default useAppointments;