import { useCallback } from "react";
import { useMaterialStore } from "../../../../../store/simulation/useMaterialStore";
import { useProductStore } from "../../../../../store/simulation/useProductStore";
import { useSelectedProduct } from "../../../../../store/simulation/useSimulationStore";
import { useVehicleStore } from "../../../../../store/simulation/useVehicleStore";

export function useTravelHandler() {
    const { getMaterialById } = useMaterialStore();
    const { getModelUuidByActionUuid } = useProductStore();
    const { selectedProduct } = useSelectedProduct();
    const { incrementVehicleLoad, addCurrentMaterial } = useVehicleStore();

    const travelLogStatus = (materialUuid: string, status: string) => {
        // console.log(`${materialUuid}, ${status}`);
    }

    const handleTravel = useCallback((action: VehicleAction, materialId?: string) => {
        if (!action || action.actionType !== 'travel' || !materialId) return;

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

        const modelUuid = getModelUuidByActionUuid(selectedProduct.productId, action.actionUuid);

        if (!modelUuid) return;

        incrementVehicleLoad(modelUuid, 1);
        addCurrentMaterial(modelUuid, material.materialType, material.materialId);

        travelLogStatus(material.materialId, `is triggering travel from ${modelUuid}`);

    }, [getMaterialById, getModelUuidByActionUuid]);

    return {
        handleTravel,
    };
}