import React from "react";
import { Text } from "@mantine/core";
import { modals } from "@mantine/modals";
import useAttributesCrud from "@/app/module-editor/[moduleId]/hooks/useAttributesCrud";
import { AttributeSchema, ModuleSchemaOutput } from "@/lib/client";

export type AttributesActions = {
    addAttributeToModule: (attribute: AttributeSchema) => void;
    removeAttributeFromModule: (attribute: AttributeSchema) => void;
    onEditAttributeLabel: (attribute: AttributeSchema) => void;
    onCreate: () => void;
    onUpdate: (attribute: AttributeSchema) => void;
    onDelete: (attribute: AttributeSchema) => void;
};

type Props = {
    module?: ModuleSchemaOutput;
    refetchModule?: () => void;
    refetchAttributes: () => void;
};

const useAttributesActions = (props: Props): AttributesActions => {
    const crud = useAttributesCrud(props);

    const addAttributeToModule = (attribute: AttributeSchema) =>
        crud.onToggleAttributeInModule(attribute, true);

    const removeAttributeFromModule = (attribute: AttributeSchema) => {
        modals.openConfirmModal({
            title: "Удаление атрибута из модуля",
            children: (
                <Text>
                    Вы уверены, что хотите удалить атрибут "{attribute.label}"
                    из модуля?
                </Text>
            ),
            confirmProps: { color: "red" },
            onConfirm: () => crud.onToggleAttributeInModule(attribute, false),
        });
    };

    const onEditAttributeLabel = (attribute: AttributeSchema) => {
        if (!props.module) return;

        modals.openContextModal({
            modal: "enterNameModal",
            title: "Редактирование имени атрибута в модуле",
            withCloseButton: true,
            innerProps: {
                onChange: values =>
                    crud.onUpdateLabel(attribute.id, values.name),
                value: { name: attribute.label },
            },
        });
    };

    const onCreate = () => {
        modals.openContextModal({
            modal: "attributeEditorModal",
            title: "Создание атрибута",
            withCloseButton: true,
            innerProps: {
                onCreate: crud.onCreate,
                isEditing: false,
            },
        });
    };

    const onUpdate = (attribute: AttributeSchema) => {
        modals.openContextModal({
            modal: "attributeEditorModal",
            title: "Редактирование атрибута",
            withCloseButton: true,
            innerProps: {
                onChange: values => crud.onUpdate(attribute.id, values),
                entity: attribute,
                isEditing: true,
            },
        });
    };

    const onDelete = (attribute: AttributeSchema) => {
        modals.openConfirmModal({
            title: "Удаление атрибута",
            children: (
                <Text>
                    Вы уверены, что хотите удалить атрибут "{attribute.label}"?
                </Text>
            ),
            confirmProps: { color: "red" },
            onConfirm: () => crud.onDelete(attribute.id),
        });
    };

    return {
        addAttributeToModule,
        removeAttributeFromModule,
        onEditAttributeLabel,
        onCreate,
        onUpdate,
        onDelete,
    };
};

export default useAttributesActions;
