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";
import {useDispatch} from "react-redux";
import {
    setSelectedAppointment,
    setSelectedScheduledAppointment,
    openModal as openAppointmentsListModal,
    openScheduledModal,
} from "../../../Redux/Slices/appointmentsSlice.js";
import {
    openModal as openPatientModal,
} from "../../../Redux/Slices/patientsSlice.js";
import dayjs from "dayjs";
import isBetween from "dayjs/plugin/isBetween";
import {useGetAppointmentTypesQuery} from "../../../Api/appointmentTypesApi.js"; // Import isBetween plugin

dayjs.extend(isBetween); // Extend dayjs with isBetween

const useHomePage = () => {
    const dispatch = useDispatch();

    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,
    });

    const {
        data: appointmentTypes = [],
        isLoading: isLoadingAppointmentTypes,
        isError: isErrorAppointmentTypes,
    } = useGetAppointmentTypesQuery(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",
            });
        }
        if (isErrorAppointmentTypes) {
            notification.error({
                message: "Ошибка",
                description: "Ошибка загрузки типов приемов.",
                placement: "topRight",
            });
        }
    }, [isErrorAppointments, isErrorScheduledAppointments, isErrorPatients, isErrorAppointmentTypes]);

    const handleEventClick = (event) => {
        if (event.appointment_datetime) {
            dispatch(setSelectedAppointment(event));
        } else {
            dispatch(setSelectedScheduledAppointment(event));
        }
    };

    const handleCreateAppointment = () => {
        dispatch(openAppointmentsListModal());
    };

    const handleCreateScheduledAppointment = () => {
        dispatch(openScheduledModal());
    };

    const handleCreatePatient = () => {
        dispatch(openPatientModal());
    };

    return {
        patients,
        appointments,
        scheduledAppointments,
        appointmentTypes,
        isLoading:
            isLoadingAppointments ||
            isLoadingScheduledAppointments ||
            isLoadingPatients ||
            isLoadingAppointmentTypes,
        isError:
            isErrorAppointments ||
            isErrorScheduledAppointments ||
            isErrorPatients ||
            isErrorAppointmentTypes,
        handleEventClick,
        handleCreateAppointment,
        handleCreateScheduledAppointment,
        handleCreatePatient,
    };
};

export default useHomePage;