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 refetch = async () => {
        if (!selectedDeal) return
        return DealService.getDealById({dealId: selectedDeal.id}).then((deal) => {
            setSelectedDeal(deal);
        })
    }
    return {
        deal: selectedDeal,
        refetch
    }
}

const useDealServicesState = (): CRUDTableProps<DealServiceSchema> => {
    const {deal, refetch} = useDealState();
    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 refetch();
        })
    }
    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 refetch();
        })
    }
    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 refetch();
        })
    }
    return {
        items: deal?.services || [],
        onCreate,
        onDelete,
        onChange
    }
}

const useDealProductsState = (): CRUDTableProps<DealProductSchema> => {
    const {deal, refetch} = useDealState();
    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 refetch();
        })
    };
    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 refetch();
        })
    };
    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 refetch();
        })
    };
    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;