import { useCallback } from "react";
import { useSceneContext } from "../../../../scene/sceneContext";
import { useProductContext } from "../../../products/productContext";

export function useManufacturerHandler() {
    const { materialStore, humanStore, productStore } = useSceneContext();
    const { getMaterialById } = materialStore();
    const { getModelUuidByActionUuid } = productStore();
    const { selectedProductStore } = useProductContext();
    const { selectedProduct } = selectedProductStore();
    const { incrementHumanLoad, addCurrentMaterial, addCurrentAction } = humanStore();

    const manufactureLogStatus = (materialUuid: string, status: string) => {
        echo.info(`${materialUuid}, ${status}`);
    }

    const handleManufacturer = useCallback((action: HumanAction, materialId?: string) => {
        if (!action || action.actionType !== 'manufacturer' || !materialId) return;

        const material = getMaterialById(materialId);
        if (!material) return;

        const modelUuid = getModelUuidByActionUuid(selectedProduct.productUuid, action.actionUuid);
        if (!modelUuid) return;

        incrementHumanLoad(modelUuid, 1);
        addCurrentAction(modelUuid, action.actionUuid);
        addCurrentMaterial(modelUuid, material.materialType, material.materialId);

        manufactureLogStatus(material.materialName, `performing manufacturer action`);

    }, [getMaterialById]);

    return {
        handleManufacturer,
    };
}