import { CRUDTableProps } from "../../../../../types/CRUDTable.tsx";
import { CardService, CardServiceSchema, CardProductSchema } from "../../../../../client";
import { useCardPageContext } from "../../../../../pages/CardsPage/contexts/CardPageContext.tsx";
import { notifications } from "../../../../../shared/lib/notifications.ts";

const useCardState = () => {

    const { selectedCard, setSelectedCard } = useCardPageContext();
    const recalculate = async () => {
        return CardService.recalculateCardPrice({
            requestBody: {
                cardId: selectedCard?.id || -1,
            },
        });
    };
    const refetchCard = async () => {
        if (!selectedCard) return;

        return CardService.getCardById({ cardId: selectedCard.id }).then(
            async card => {
                setSelectedCard(card);
            },
        );
    };
    const refetch = async () => {
        if (!selectedCard) return;
        await refetchCard();
        const { ok, message } = await recalculate();
        if (!ok) notifications.guess(ok, { message });

        await refetchCard();
    };
    return {
        card: selectedCard,
        refetch,
    };
};

const useCardServicesState = (): CRUDTableProps<CardServiceSchema> => {
    const { card, refetch } = useCardState();
    const refetchAndRecalculate = async () => {
        await refetch();
    };
    const onCreate = (item: CardServiceSchema) => {
        if (!card) return;
        CardService.addCardService({
            requestBody: {
                cardId: card.id,
                serviceId: item.service.id,
                quantity: item.quantity,
                price: item.price,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    const onDelete = (item: CardServiceSchema) => {
        if (!card) return;
        CardService.deleteCardService({
            requestBody: {
                cardId: card.id,
                serviceId: item.service.id,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    const onChange = (item: CardServiceSchema) => {
        if (!card) return;
        CardService.updateCardService({
            requestBody: {
                cardId: card.id,
                service: item,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    return {
        items: card?.services || [],
        onCreate,
        onDelete,
        onChange,
    };
};

const useDealProductsState = (): CRUDTableProps<CardProductSchema> => {
    const { card, refetch } = useCardState();
    const refetchAndRecalculate = async () => {
        await refetch();
    };
    const onCreate = (item: CardProductSchema) => {
        if (!card) return;
        CardService.addCardProduct({
            requestBody: {
                cardId: card.id,
                product: item,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    const onDelete = (item: CardProductSchema) => {
        if (!card) return;
        CardService.deleteCardProduct({
            requestBody: {
                cardId: card.id,
                productId: item.product.id,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    const onChange = (item: CardProductSchema) => {
        if (!card) return;
        CardService.updateCardProduct({
            requestBody: {
                cardId: card.id,
                product: item,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    return {
        items: card?.products || [],
        onCreate,
        onDelete,
        onChange,
    };
};
const useCardProductAndServiceTabState = () => {
    const cardState = useCardState();
    const cardProductsState = useDealProductsState();
    const cardServicesState = useCardServicesState();
    return {
        cardState,
        cardProductsState,
        cardServicesState,
    };
};
export default useCardProductAndServiceTabState;
