import { CRUDTableProps } from "../../../../../types/CRUDTable.tsx";
import { DealProductSchema, DealService, DealServiceSchema } from "../../../../../client";
import { useDealPageContext } from "../../../contexts/DealPageContext.tsx";
import { notifications } from "../../../../../shared/lib/notifications.ts";

const useDealState = () => {

    const { selectedDeal, setSelectedDeal } = useDealPageContext();
    const recalculate = async () => {
        return DealService.recalculateDealPrice({
            requestBody: {
                dealId: selectedDeal?.id || -1,
            },
        });
    };
    const refetchDeal = async () => {
        if (!selectedDeal) return;

        return DealService.getDealById({ dealId: selectedDeal.id }).then(
            async deal => {
                setSelectedDeal(deal);
            },
        );
    };
    const refetch = async () => {
        if (!selectedDeal) return;
        await refetchDeal();
        const { ok, message } = await recalculate();
        if (!ok) notifications.guess(ok, { message });

        await refetchDeal();
    };
    return {
        deal: selectedDeal,
        refetch,
    };
};

const useDealServicesState = (): CRUDTableProps<DealServiceSchema> => {
    const { deal, refetch } = useDealState();
    const refetchAndRecalculate = async () => {
        await refetch();
    };
    const onCreate = (item: DealServiceSchema) => {
        if (!deal) return;
        DealService.addDealService({
            requestBody: {
                dealId: deal.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: DealServiceSchema) => {
        if (!deal) return;
        DealService.deleteDealService({
            requestBody: {
                dealId: deal.id,
                serviceId: item.service.id,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    const onChange = (item: DealServiceSchema) => {
        if (!deal) return;
        DealService.updateDealService({
            requestBody: {
                dealId: deal.id,
                service: item,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    return {
        items: deal?.services || [],
        onCreate,
        onDelete,
        onChange,
    };
};

const useDealProductsState = (): CRUDTableProps<DealProductSchema> => {
    const { deal, refetch } = useDealState();
    const refetchAndRecalculate = async () => {
        await refetch();
    };
    const onCreate = (item: DealProductSchema) => {
        if (!deal) return;
        DealService.addDealProduct({
            requestBody: {
                dealId: deal.id,
                product: item,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    const onDelete = (item: DealProductSchema) => {
        if (!deal) return;
        DealService.deleteDealProduct({
            requestBody: {
                dealId: deal.id,
                productId: item.product.id,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    const onChange = (item: DealProductSchema) => {
        if (!deal) return;
        DealService.updateDealProduct({
            requestBody: {
                dealId: deal.id,
                product: item,
            },
        }).then(async ({ ok, message }) => {
            if (!ok) notifications.guess(ok, { message });
            if (ok) await refetchAndRecalculate();
        });
    };
    return {
        items: deal?.products || [],
        onCreate,
        onDelete,
        onChange,
    };
};
const useDealProductAndServiceTabState = () => {
    const dealState = useDealState();
    const dealProductsState = useDealProductsState();
    const dealServicesState = useDealServicesState();
    return {
        dealState,
        dealProductsState,
        dealServicesState,
    };
};
export default useDealProductAndServiceTabState;
