import { db } from "../../../../database/dbclient.js";
import { ocmeData } from "../../../../database/schema/ocme.js";
import { differenceInMinutes } from "date-fns";
import { createLog } from "../../logger/logger.js";
import { eq } from "drizzle-orm";
import { timeZoneFix } from "../../../globalUtils/timeZoneFix.js";

export const getInfo = async () => {
    let ocmeInfo: any = [];
    try {
        ocmeInfo = await db
            .select()
            .from(ocmeData)
            .where(eq(ocmeData.pickedUp, false));

        // add in the time difference
        ocmeInfo = ocmeInfo.map((o: any) => {
            const now = new Date(Date.now());
            //const strippedDate = o.add_Date.replace("Z", "");
            const diff = differenceInMinutes(timeZoneFix(), o.add_Date);
            return { ...o, waitingFor: diff };
        });
        createLog(
            "debug",
            "ocme",
            "ocme",
            `There are ${ocmeInfo.length} pallet(s) to be picked up.`
        );
    } catch (error) {
        createLog(
            "error",
            "ocme",
            "ocme",
            "There was an error trying to retrive the ocmeInfo."
        );
        throw Error("There was an error trying to retrive the.");
    }

    return ocmeInfo;
};
