import UseObjectState from "../../../types/UseObjectState.ts";
import {
    type ServicePriceCategorySchema,
    ServiceService,
} from "../../../client";
import useServicePriceCategoriesList from "./useServicePriceCategoriesList.tsx";
import { modals } from "@mantine/modals";
import { notifications } from "../../../shared/lib/notifications.ts";

const useServicePriceCategoryState =
    (): UseObjectState<ServicePriceCategorySchema> => {
        const { objects, refetch } = useServicePriceCategoriesList();
        const onCreateClick = () => {
            modals.openContextModal({
                modal: "servicePriceCategoryForm",
                title: "Создание категории цен",
                withCloseButton: false,
                innerProps: {
                    onCreate,
                },
            });
        };
        const onCreate = (values: ServicePriceCategorySchema) => {
            console.log(ServiceService);
            ServiceService.createPriceCategory({
                requestBody: {
                    name: values.name,
                },
            }).then(async ({ ok, message }) => {
                notifications.guess(ok, { message: message });
                if (!ok) return;
                await refetch();
            });
        };
        const onDelete = (item: ServicePriceCategorySchema) => {
            modals.openConfirmModal({
                title: "Удаление категории",
                children: "Вы уверены, что хотите удалить категорию?",
                onConfirm: () => {
                    ServiceService.deletePriceCategory({
                        requestBody: {
                            id: item.id,
                        },
                    }).then(async ({ ok, message }) => {
                        notifications.guess(ok, { message: message });
                        if (!ok) return;
                        await refetch();
                    });
                },
            });
        };
        const onChange = (item: ServicePriceCategorySchema) => {
            modals.openContextModal({
                modal: "servicePriceCategoryForm",
                title: "Изменение категории цен",
                withCloseButton: false,
                innerProps: {
                    onChange: (values: ServicePriceCategorySchema) => {
                        ServiceService.updatePriceCategory({
                            requestBody: {
                                id: item.id,
                                name: values.name,
                            },
                        }).then(async ({ ok, message }) => {
                            notifications.guess(ok, { message: message });
                            if (!ok) return;
                            await refetch();
                        });
                    },
                    element: item,
                },
            });
        };

        return {
            onCreateClick,
            onCreate,
            onDelete,
            onChange,
            objects,
        };
    };
export default useServicePriceCategoryState;
