import { CardGroupService, CardService, CardSummary } from "../../../../client";
import { useCardPageContext } from "../../../../pages/CardsPage/contexts/CardPageContext.tsx";
import { modals } from "@mantine/modals";
import { Text } from "@mantine/core";
import { notifications } from "../../../../shared/lib/notifications.ts";

const useCardSummaryState = () => {
    const { refetchCards } = useCardPageContext();

    const recalculate = async (cardId: number) => {
        return CardService.recalculateCardPrice({
            requestBody: {
                cardId,
            },
        }).then(({ ok, message }) => {
            notifications.guess(ok, { message });
        });
    };

    const onDelete = (summary: CardSummary) => {
        modals.openConfirmModal({
            title: "Удаление карточки",
            children: (
                <Text>
                    Вы уверены что хотите удалить карточку "{summary.name}"?
                </Text>
            ),
            labels: { confirm: "Да", cancel: "Нет" },
            confirmProps: { color: "red" },
            onConfirm: () => {
                CardService.deleteCard({
                    requestBody: {
                        cardId: summary.id,
                    },
                }).then(async (response) => {
                    notifications.guess(response.ok, { message: response.message });
                    if (response.ok) await refetchCards();
                });
            },
        });
    };

    const onComplete = (summary: CardSummary) => {
        modals.openConfirmModal({
            title: "Удаление карточки",
            children: (
                <Text>
                    Вы уверены что хотите завершить карточку "{summary.name}"?
                </Text>
            ),
            labels: { confirm: "Да", cancel: "Нет" },
            confirmProps: { color: "green" },
            onConfirm: () => {
                CardService.completeCard({
                    requestBody: {
                        cardId: summary.id,
                    },
                }).then(async (response) => {
                    notifications.guess(response.ok, { message: response.message });
                    if (response.ok) await refetchCards();
                });
            },
        });
    };

    const onDeleteFromGroup = (summary: CardSummary) => {
        modals.openConfirmModal({
            title: "Удаление карточки",
            children: (
                <Text>
                    Вы уверены что хотите удалить карточку "{summary.name}" из группы?
                </Text>
            ),
            labels: { confirm: "Да", cancel: "Нет" },
            confirmProps: { color: "red" },
            onConfirm: () => {
                CardGroupService.removeCard({
                    requestBody: {
                        cardId: summary.id,
                    },
                }).then(async (response) => {
                    notifications.guess(response.ok, { message: response.message });
                    if (response.ok) await refetchCards();
                    await recalculate(summary.id);
                    if (response.ok) await refetchCards();
                });
            },
        });
    };

    return {
        onDelete,
        onComplete,
        onDeleteFromGroup,
    };
};

export default useCardSummaryState;