import axios from "axios";
import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js";
import { lstAuth } from "../../../../index.js";
import { createLog } from "../../../logger/logger.js";
import { db } from "../../../../../database/dbclient.js";
import { prodlabels } from "../../../../../database/schema/prodLabels.js";
import { eq, sql } from "drizzle-orm";

export const bookInLabel = async (data: any) => {
    // update sscc so we can book in
    const SSCC = data.SSCC.slice(2);

    // api url
    const url = await prodEndpointCreation(
        "/public/v1.1/Manufacturing/ProductionControlling/BookIn"
    );

    // create bookin
    const newBookin = {
        scannerId: 777,
        sscc: SSCC,
    };

    try {
        const res = await axios.post(url, newBookin, {
            headers: {
                Authorization: `Basic ${lstAuth}`,
                accept: "text/plain",
            },
        });

        if (res.status != 200) {
            createLog(
                "error",
                "labeling",
                "ocp",
                `${
                    data.printer ? data.printer[0].name : "Manual book in"
                }, Error:${res.data}`
            );
            //printerUpdate(data.printer, 7, "Error while booking in.");

            return {
                success: false,
                message: "There was an error booking in the label.",
                data: res.data,
            };
        }
        // update the label.
        try {
            const booked = await db
                .update(prodlabels)
                .set({
                    status: "Booked in",
                    upd_date: sql`NOW()`,
                })
                .where(
                    eq(prodlabels.runningNr, parseInt(data.SSCC.slice(10, -1)))
                )
                .returning({ runningNr: prodlabels.runningNr });

            createLog(
                "info",
                "labeling",
                "ocp",
                `${parseInt(data.SSCC.slice(10, -1))} , was just booked in.`
            );

            return {
                success: true,
                message: `${parseInt(
                    data.SSCC.slice(10, -1)
                )}, was just booked in`,
                data: { SSCC: data.SSCC },
            };
        } catch (error) {
            //console.log(error);
            createLog(
                "error",
                "labeling",
                "ocp",
                `Error creating new runningNumber in the DB.`
            );
            return {
                success: true,
                message: `${parseInt(
                    data.SSCC.slice(10, -1)
                )}, encoutnered an error posting to the db`,
                data: error,
            };
        }
    } catch (error: any) {
        createLog(
            "error",
            "labeling",
            "ocp",
            `${
                data.printer ? data.printer[0].name : "Manual book in"
            }, "Error: ${error}`
        );
        // console.log(error.response.data);
        return {
            success: false,
            message: "There was an error booking in the label.",
            data: error.response.data,
        };
    }
};
