// full lane count
import axios from "axios";
import { delay } from "../../../../globalUtils/delay.js";
import { createLog } from "../../../logger/logger.js";
import { openLane, prepareLane, scannerID } from "../cycleCount.js";
import type { User } from "../../../../types/users.js";
import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js";

let delayTime = 100;

export const fullLaneCount = async (
    user: User,
    lane: string,
    ocmeLanes: any
) => {
    // prepare the lane.
    try {
        const openlane = await axios({
            method: "POST",
            url: prepareLane,
            headers: {
                Authorization: `Basic ${user.prod}`,
                "Content-Type": "application/json",
            },
            data: {
                scannerId: scannerID,
                laneId: lane,
            },
        });

        createLog("info", user.username!, "ocme-count", openlane.data.message);
        try {
            const open = await axios({
                method: "POST",
                url: openLane,
                headers: {
                    Authorization: `Basic ${user.prod}`,
                    "Content-Type": "application/json",
                },
                data: {
                    scannerId: scannerID,
                    laneId: lane,
                },
            });

            createLog("info", user.username!, "ocme-count", open.data.Message);
        } catch (error) {
            console.log(error);
        }
    } catch (error) {
        console.log(error);
    }

    // do the inv

    for (let i = 0; i < ocmeLanes.length; i++) {
        const count = {
            scannerId: scannerID,
            sscc: ocmeLanes[i].sscc,
        };
        //createLog("cyclecounting", "info", `Processing running: ${ocmeLanes[i].runningNumber}`);
        await delay(delayTime);

        try {
            const openLane = await axios({
                method: "POST",
                url: await prodEndpointCreation(
                    "/public/v1.0/Warehousing/InventoryCount"
                ),
                headers: {
                    Authorization: `Basic ${user.prod}`,
                    "Content-Type": "application/json",
                },
                data: count,
            });

            createLog(
                "info",
                user.username!,
                "ocme-count",
                `${openLane.data.Message} on running: ${ocmeLanes[i].runningNumber}`
            );
            await delay(delayTime);
        } catch (error) {
            createLog("error", user.username!, "ocme-count", `${error}`);
        }
    }

    // close the count
    // close the order
    try {
        const openLane = await axios({
            method: "POST",
            url: await prodEndpointCreation(
                "/public/v1.0/Warehousing/InventoryClose"
            ),
            headers: {
                Authorization: `Basic ${user.prod}`,
                "Content-Type": "application/json",
            },
            data: {
                scannerId: scannerID,
                laneId: lane,
            },
        });

        createLog("info", user.username!, "ocme-count", openLane.data.Message);

        if (openLane.data.Result === 0) {
            //release the lane
            //----------------------------------------------------
            try {
                const openLane = await axios({
                    method: "POST",
                    url: await prodEndpointCreation(
                        "/public/v1.1/Warehousing/ReleaseLaneFromInventory"
                    ),
                    headers: {
                        Authorization: `Basic ${user.prod}`,
                        "Content-Type": "application/json",
                    },
                    data: {
                        laneId: lane,
                    },
                });

                createLog(
                    "info",
                    user.username!,
                    "ocme-count",
                    openLane.data.message
                );
            } catch (error) {
                createLog("error", user.username!, "ocme-count", `${error}`);
            }
        }
    } catch (error) {
        createLog("error", user.username!, "ocme-count", `${error}`);
    }

    return { success: true, message: `Lane completed` };
};
