import { useDispatch, useSelector } from "react-redux";
import { setSelectedAppointment } from "../../../Redux/Slices/appointmentsSlice.js";
import dayjs from "dayjs";

const useAppointmentViewUI = () => {
    const dispatch = useDispatch();
    const { selectedAppointment } = useSelector((state) => state.appointmentsUI);

    const modalWidth = 700;
    const blockStyle = { marginBottom: 16 };
    const footerRowStyle = { marginTop: 16 };
    const footerButtonStyle = { marginRight: 8 };

    const labels = {
        title: "Просмотр приема",
        patient: "Пациент:",
        birthday: "Дата рождения:",
        email: "Email:",
        phone: "Телефон:",
        type: "Тип приема:",
        appointmentTime: "Время приема:",
        daysUntilNext: "Дней до следующего приема:",
        results: "Результаты приема:",
        closeButton: "Закрыть",
        notSpecified: "Не указан",
        resultsNotSpecified: "Не указаны",
    };

    const visible = !!selectedAppointment;

    const getDateString = (date) => {
        return date ? new Date(date).toLocaleDateString("ru-RU") : labels.notSpecified;
    };

    const getAppointmentTime = (datetime) => {
        return datetime
            ? dayjs(datetime).format("DD.MM.YYYY HH:mm")
            : labels.notSpecified;
    };

    const getPatientName = (patient) => {
        return patient
            ? `${patient.last_name} ${patient.first_name}`
            : labels.notSpecified;
    };

    const getPatientField = (field) => {
        return field || labels.notSpecified;
    };

    const getResults = (results) => {
        return results || labels.resultsNotSpecified;
    };

    const onCancel = () => {
        dispatch(setSelectedAppointment(null));
    };

    return {
        modalWidth,
        blockStyle,
        footerRowStyle,
        footerButtonStyle,
        labels,
        selectedAppointment,
        visible,
        getDateString,
        getAppointmentTime,
        getPatientName,
        getPatientField,
        getResults,
        onCancel,
    };
};

export default useAppointmentViewUI;