import { DealService, DealSummary } from "../../../client";
import { useDealPageContext } from "../../../pages/LeadsPage/contexts/DealPageContext.tsx";
import { modals } from "@mantine/modals";
import { Text } from "@mantine/core";
import { notifications } from "../../../shared/lib/notifications.ts";

const useDealSummaryState = () => {
    const { refetchDeals } = useDealPageContext();

    const recalculate = async (dealId: number) => {
        return DealService.recalculateDealPrice({
            requestBody: {
                dealId: dealId,
            },
        }).then(({ ok, message }) => {
            notifications.guess(ok, { message });
        });
    };

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

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


                });
            },
        });
    };

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

                });
            },
        });
    };

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

export default useDealSummaryState;