import axios from "axios";
import {closeLane, openLane, prepareLane, releaseLane, scannerID} from "../cycleCount.js";
import {createLog} from "../../../logger/logger.js";
import type {User} from "../../../../types/users.js";

export const emptyCount = async (user: User, lane: string) => {
    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);

        // start the empty inventory process
        if (openlane.data.result === 0) {
            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", openlane.data.message);

                if (open.data.Result === 0) {
                    //consider the lane empty and close it
                    try {
                        const closelane = await axios({
                            method: "POST",
                            url: closeLane,
                            headers: {
                                Authorization: `Basic ${user.prod}`,
                                "Content-Type": "application/json",
                            },
                            data: {
                                scannerId: scannerID,
                                laneId: lane,
                            },
                        });

                        createLog("info", user.username!, "ocme-count", openlane.data.message);

                        if (closelane.data.Result === 0) {
                            //release the lane
                            //----------------------------------------------------
                            try {
                                const close = await axios({
                                    method: "POST",
                                    url: releaseLane,
                                    headers: {
                                        Authorization: `Basic ${user.prod}`,
                                        "Content-Type": "application/json",
                                    },
                                    data: {
                                        laneId: lane,
                                    },
                                });

                                createLog("info", user.username!, "ocme-count", close.data.message);
                            } catch (error) {
                                createLog("error", user.username!, "ocme-count", "Releasing the lane");
                            }
                        }
                    } catch (error) {
                        createLog("error", user.username!, "ocme-count", "Closing the lane");
                    }
                }
            } catch (error) {
                createLog("error", user.username!, "ocme-count", "Opening the lane");
            }
        }
    } catch (error) {
        createLog("error", user.username!, "ocme-count", "Preparing the lane");
    }
};
